Unique identifier for NSDocument

Q: Is there a way to get a unique NSDocument identifier in a Document-based ™ application that is saved during application restart?



Note1: there should definitely be some kind of identifier, which is not just fileURL , but something else, because the application works well with autosave and not even saved documents.

Note2: for some reason, I do not want to generate and save this identifier for the content document.

+4
source share
2 answers

encodeRestorableStateWithCoder restoreDocumentWindowWithIdentifier .

- (void) encodeRestorableStateWithCoder: (NSCoder *) coder {
    [super encodeRestorableStateWithCoder: coder];
    [coder encodeObject: @{@"color":@"red"} forKey: @"OBJECT_KEY"];
}

- (void) restoreDocumentWindowWithIdentifier: (NSString *) identifier
                                       state: (NSCoder *) state
                           completionHandler: (void (^) (NSWindow *, NSError *)) completionHandler {
    NSDictionary * restoredInfo = [state decodeObjectForKey: @"OBJECT_KEY"];
    [super restoreDocumentWindowWithIdentifier: identifier state: state completionHandler: completionHandler];
}

, UID, .

0

, , :

uuid_t uuid;
[[NSUUID new] getUUIDBytes:uuid];

int result = setxattr( path, "com.yourcompany.yourapp.yourattr", &uuid, sizeof(uuid), 0, 0);

, , , .

+2

Source: https://habr.com/ru/post/1667091/


All Articles