user1698875
user1698875

Reputation: 153

[NSTaggedPointerString managedObjectContext]: unrecognized selector

I am trying to save additions to a data model using Swift 3. I am getting errors, -[NSTaggedPointerString managedObjectContext]: unrecognized selector sent to instance 0xa00e8808201a0cc8. I have checked the data model and all items referred to in the save are consistent with the data model. My save code is:

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

The data model is :

[enter image description here][1

I have used this as my coreDataStack:

import Foundation

import CoreData

class 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()
        }
    }
}

}

I cannot see any inconsistencies that would cause the unrecognised selector, what may I be missing?

Upvotes: 1

Views: 428

Answers (1)

vadian
vadian

Reputation: 285082

The line

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

causes the error. According to your model the value for key transectTitle must be a managed object instance of Transect rather than a string.

Upvotes: 1

Related Questions