![](/img/trans.png)
[英]Swift CoreData: error: Failed to call designated initializer on NSManagedObject class 'NSManagedObject'
[英]Core Data Error when segueing: Failed to call designated initializer on NSManagedObject class
我有一個視圖控制器,當點擊mapView上的圖釘時,它會與另一個視圖控制器相連。 “ Pin”是一個NSManagedObject,它在被發送到第二個視圖控制器之前(在其中函數使用實際圖像填充),在其中填充了緯度和經度。
我的didSelect函數完成后並且在我的performSegue函數被調用之前,出現錯誤“ CoreData:錯誤:無法在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聲明為可選,並用“!”強制聲明。 這兩個選項均無效。 根據我在網上學習和閱讀的許多文章,我將需要使用指定的初始化程序來初始化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)
...並執行以下命令:
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)")
}
}
我想念什么?
我通過在類的開頭使變量聲明為可選來解決此問題:
var selectedPin: Pin?
...並在我的長按手勢中以不同的方式初始化Recognizer函數:
@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)")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.