segueing时出现核心数据错误:无法在NSManagedObject类上调用指定的初始值设定项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了segueing时出现核心数据错误:无法在NSManagedObject类上调用指定的初始值设定项相关的知识,希望对你有一定的参考价值。

我有一个视图控制器,当点击mapView上的一个引脚时,它会转到另一个视图控制器。 “Pin”是一个NSManagedObject,它在被发送到第二个视图控制器之前会被填充纬度和经度(其中一个函数用实际图像填充它)。

在我的didSelect函数完成之后和调用performSegue函数之前,我收到错误“CoreData:error:无法在NSManagedObject类'Pin'上调用指定的初始值设定项”。

@nonobjc public class func fetchRequest() -> NSFetchRequest<Pin> {
    return NSFetchRequest<Pin>(entityName: "Pin")
}

@NSManaged public var latitude: Double
@NSManaged public var longitude: Double
@NSManaged public var images: NSSet?

这是班级:

convenience init(latitude: Double, longitude: Double, context: NSManagedObjectContext) {
    if let entity = NSEntityDescription.entity(forEntityName: "Pin", in: context) {
        self.init(entity: entity, insertInto: context)
        self.latitude = latitude
        self.longitude = longitude
    } else {
        fatalError("Unable to find entity name")
    }
}

我已经尝试将引脚声明为可选,并强制用“!”声明它。这两个选项都不起作用。根据我在线学习和阅读的内容,我需要使用指定的初始化程序初始化pin变量。这是我在视图控制器类顶部的尝试,但它也不起作用。

var pin = Pin.init(entity: NSEntityDescription.entity(forEntityName: "Pin", in: CoreDataStack.sharedInstance().context)!, insertInto: CoreDataStack.sharedInstance().context)

创建引脚:

    // Create the annotation
    let touchPoint = gestureRecognizer.location(in: mapView)
    let newCoordinate = self.mapView.convert(touchPoint, toCoordinateFrom:self.mapView)
    let annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinate


    pin = Pin(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude, context: CoreDataStack.sharedInstance().context)
    let pinAnnotation = PinAnnotation(objectID: pin.objectID, title: nil, subtitle: nil, coordinate: annotation.coordinate)

    // Add the annotation
    mapView.addAnnotation(pinAnnotation)
    CoreDataStack.sharedInstance().saveContext()

在didSelect中选择引脚:

do {
        let pinAnnotation = view.annotation as! PinAnnotation
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Pin")
        let predicate = NSPredicate(format: "latitude == %@ AND longitude == %@", argumentArray: [pinAnnotation.coordinate.latitude, pinAnnotation.coordinate.longitude])
        fetchRequest.predicate = predicate
        let pins = try CoreDataStack.sharedInstance().context.fetch(fetchRequest) as? [Pin]
        pin = pins![0]
    } catch let error as NSError {
        print("failed to get pin by object id")
        print(error.localizedDescription)
        return
    }
        self.performSegue(withIdentifier: "collectionViewSegue", sender: self)

......并执行segue:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "collectionViewSegue" {
        let controller = segue.destination as! CollectionViewController
        print("CoreDataStack context in segue= (CoreDataStack.sharedInstance().context)")
            controller.selectedPin = pin
            if let images = pin.images?.allObjects as? [Images] {
                controller.photos = images
            }
        print("PrepareForSegue pin properties are: 
 latitude: (pin.latitude) 
 longitude: (pin.longitude)")
        }
}

我错过了什么?

答案

我通过在我的类的开头使变量声明可选来解决这个问题:

var selectedPin: Pin?

...并在我的长按gestureRecognizer功能中进行不同的初始化:

    @objc func handleLongPress(_ gestureRecognizer : UIGestureRecognizer) {
    if gestureRecognizer.state != .began { return }
    print("Tap gesture recognized")

    // Create the annotation
    let touchPoint = gestureRecognizer.location(in: mapView)
    let newCoordinate = self.mapView.convert(touchPoint, toCoordinateFrom:self.mapView)
    let annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinate

    // Initialize NSManagedObject 'Pin' with properties
    selectedPin = Pin(context: CoreDataStack.sharedInstance().context)
    selectedPin?.latitude = annotation.coordinate.latitude
    selectedPin?.longitude = annotation.coordinate.longitude

    if let selectedPin = selectedPin {
    let pinAnnotation = PinAnnotation(objectID: selectedPin.objectID, title: nil, subtitle: nil, coordinate: annotation.coordinate)

    // Add the annotation
    mapView.addAnnotation(pinAnnotation)
    }

    CoreDataStack.sharedInstance().saveContext()
    print("This is what the ole save looks like: (CoreDataStack.sharedInstance().context)")
}

以上是关于segueing时出现核心数据错误:无法在NSManagedObject类上调用指定的初始值设定项的主要内容,如果未能解决你的问题,请参考以下文章

使用核心数据时出现不一致错误

segueing 时的核心数据错误:无法在 NSManagedObject 类上调用指定的初始化程序

在opencv c ++中查找图像卷积时出现分段错误(核心转储)错误

核心数据:执行提取时出现错误的访问错误

访问共享进程内存时出现分段错误(核心转储)

在非连续版本之间迁移时出现核心数据迁移错误