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

问题描述 投票:0回答:1

我有一个视图控制器,当点击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: \n latitude: \(pin.latitude) \n longitude: \(pin.longitude)")
        }
}

我错过了什么?

ios core-data initialization nsmanagedobject
1个回答
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)")
}
© www.soinside.com 2019 - 2024. All rights reserved.