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

时间:2017-12-13 23:03:07

标签: ios core-data initialization nsmanagedobject

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

我收到错误" CoreData:错误:无法在NSManagedObject类上调用指定的初始化程序' Pin'"在我的didSelect函数完成之后,在调用performSegue函数之前。

@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声明为可选,并强制使用&#34;!&#34;声明它。这两个选项都不起作用。根据我在网上的许多帖子中学到和阅读的内容,我需要使用指定的初始化程序初始化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: \n latitude: \(pin.latitude) \n longitude: \(pin.longitude)")
        }
}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

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

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)")
}