[NSTaggedPointerString managedObjectContext]:无法识别的选择器

时间:2017-04-29 16:41:45

标签: core-data swift3

我正在尝试使用Swift 3保存对数据模型的添加。我收到错误, - [NSTaggedPointerString managedObjectContext]:无法识别的选择器发送到实例0xa00e8808201a0cc8。我检查了数据模型,保存中引用的所有项都与数据模型一致。我的保存代码是:

            let entity = NSEntityDescription.entity(forEntityName: "TransectPlants", in: coreDataStack.context)!
        let plantObservation = NSManagedObject(entity: entity, insertInto: coreDataStack.context)
        plantObservation.setValue(selectedFamilyName, forKey: "plantFamily")
        plantObservation.setValue(selectedSpeciesName, forKey: "plantSpecies")
        plantObservation.setValue(transectNameString, forKey: "transectTitle")

        do {
            try coreDataStack.context.save()
        } catch let error as NSError {
            print("Could not save. \(error), \(error.userInfo)")
        }

数据模型是:

[enter image description here] [1

我用它作为我的coreDataStack:

import Foundation

导入CoreData

类CoreDataStack {

let modelName = "TransectWalkaboutPro"

lazy var context: NSManagedObjectContext = {

    var managedObjectContext = NSManagedObjectContext(
        concurrencyType: .mainQueueConcurrencyType)

    managedObjectContext.persistentStoreCoordinator = self.psc
    return managedObjectContext
}()

fileprivate lazy var psc: NSPersistentStoreCoordinator = {

    let coordinator = NSPersistentStoreCoordinator(
        managedObjectModel: self.managedObjectModel)

    let url = self.applicationDocumentsDirectory
        .appendingPathComponent("TransectWalkaboutPro.sqlite")

    do {
        let options =
            [NSMigratePersistentStoresAutomaticallyOption : true, NSInferMappingModelAutomaticallyOption : true ]

        try coordinator.addPersistentStore(
            ofType: NSSQLiteStoreType, configurationName: nil, at: url,
            options: options)
    } catch  {
        print("Error adding persistent store.")
    }

    return coordinator
}()

fileprivate lazy var managedObjectModel: NSManagedObjectModel = {

    let modelURL = Bundle.main
        .url(forResource: self.modelName,
             withExtension: "momd")!
    return NSManagedObjectModel(contentsOf: modelURL)!

}()

fileprivate lazy var applicationDocumentsDirectory: URL = {
    let urls = FileManager.default.urls(
        for: .documentDirectory, in: .userDomainMask)
    return urls[urls.count-1]
}()

func saveContext () {
    if context.hasChanges {
        do {
            try context.save()
        } catch let error as NSError {
            print("Error: \(error.localizedDescription)")
            abort()
        }
    }
}

}

我看不出会导致无法识别的选择器的任何不一致,我可能会遗漏哪些?

1 个答案:

答案 0 :(得分:1)

该行

plantObservation.setValue(transectNameString, forKey: "transectTitle")

导致错误。根据您的模型,键transectTitle的值必须是Transect的托管对象实例,而不是字符串。