Search code examples
iosswiftcore-dataobjectidnsmanagedobjectid

managedObjectID throwing "Unresolved Identifier" Error


I'm trying to fetch a specific Item from the CoreData store, so that I can edit particular values for keys. The editing takes place in a different ViewController to where the entity to edit is picked.

I have prepare(for segue:) in the original View Controller pass the objectID for the selected entity as URL using the uriRepresenation() method of objectID. This is done since I could not figure out how to store the objectID as NSManagedObjectID (I got erros of Type mismatch)

Now that I have the URL passing between the two view controllers, the desitantion view controller has the below code:

 override func viewDidLoad() {
    toggleDatePicker()
    self.tableView.tableFooterView = UIView()
    let editableObjectID = managedObjectID(forURIRepresentation: editableObjectURL!)
    let editableEntity = context.object(with: editableObjectID!)
}

And the line:

let editableObjectID = managedObjectID(forURIRepresentation: editableObjectURL!)

throws "Use of unresolved identifier "managedObjectID""

I've ran into a wall here, and can't seem to figure this one out. The API documentation suggests this should be possible to perform but the method it references seems not to exist.

Any suggestions would be welcome.


Solution

  • managedObjectID(forURIRepresentation:) is a method on a persistentStoreCoordinator. You can get the store coordinator from a managedObjectContext (the property persistentStoreCoordinator)

        let editableObjectID = context.persistentStoreCoordinator.managedObjectID(forURIRepresentation: editableObjectURL!)