Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift 3 convert string to objectID

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?

like image 277
Ghost108 Avatar asked May 15 '17 13:05

Ghost108


1 Answers

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)
    }
like image 63
Joshua Kaden Avatar answered Nov 17 '22 04:11

Joshua Kaden