i work with swift 3 and nsoutlineview. i would like to save the objectID of a core data record into a textfield. so i have to convert it into a string:
txtFied.stringValue = "\(CoreData[outlineView.selectedRow].objectID)"
how can i convert it back to an NSManagedObjectID?
I've done this via the persistent store coordinator's managedObjectID(forURIRepresentation:)
method, as outlined below:
// Convert NSManagedObjectID to a string, via the uriRepresentation method.
let objectIDString = <your managed object ID>.uriRepresentation().absoluteString
...
// Use the persistent store coordinator to transform the string back to an NSManagedObjectID.
if let objectIDURL = URL(string: objectIDString) {
let coordinator: NSPersistentStoreCoordinator = <reference to your persistent store coordinator>
let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectIDURL)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With