gpt4 book ai didi

objective-c - 为什么 NSURL 的 NSURLDocumentIdentifierKey (几乎)总是 nil?

转载 作者:行者123 更新时间:2023-12-03 16:35:28 25 4
gpt4 key购买 nike

OSX Yosemite 在 NSURL 上引入了一个非常方便的属性:NSURLDocumentIdentifierKey

引用文档:

NSURLDocumentIdentifierKey

The document identifier returned as an NSNumber (read-only). The document identifier is a value assigned by the kernel to a file or directory. This value is used to identify the document regardless of where it is moved on a volume. The identifier persists across system restarts. It is not transferred when the file is copied, but it survives "safe save” operations. For example, it remains on the path to which it was assigned, even after calling the replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: method. Document identifiers are only unique within a single volume. This property is not supported by all volumes.

Available in OS X v10.10 and iOS 8.0.

不幸的是,该值似乎大部分为零(除了极少数看起来彼此完全脱节的例子)。

特别是,此代码将在最后一行抛出异常(在 Yosemite 10.10.3 上测试):

    NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *attributesFlags = @[NSURLNameKey, mNSURLDocumentIdentifierKey];

NSDirectoryEnumerator *en = [fileManager enumeratorAtURL:[NSURL URLWithString:NSHomeDirectory()]
includingPropertiesForKeys:attributesFlags
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:^BOOL(NSURL *url, NSError *error) {
NSAssert(NO, @"An error has occured");
return YES;
}];

for(NSURL *URL in en) {
NSNumber *documentID = nil;
NSError *error = nil;
BOOL result = [URL getResourceValue:&documentID forKey:NSURLDocumentIdentifierKey error:&error]; \
NSAssert(result == YES && error==nil, @"Unable to read property. Error: %@", error); \
NSLog(@"Processing file: %@", URL);


// This will break most of the times
NSAssert(documentID != nil, @"Document ID should not be nil!!");
}

也许我误解了文档,但在我看来 NSURLDocumentIdentifierKey 应该在磁盘上的每个文件上可用。

最佳答案

我就这个问题向 Apple 提交了错误,并收到了有关我的报告的反馈。截至今天,有关跟踪 DocumentIdentifier 的信息尚未包含在文档中,但票证仍处于开放状态。

缺少的信息是,文件系统默认情况下不跟踪DocumentIdentifier。您必须使用 chflagsUF_TRACKED 标志在要跟踪的每个文件上设置一个标志来启用跟踪。

以下脚本将打印文件的DocumentIdentifier:

https://gist.github.com/cmittendorf/fac92272a941a9cc64d5

此脚本将启用对 DocumentIdentifier 的跟踪:

https://gist.github.com/cmittendorf/b680d1a03aefa08583d7

关于objective-c - 为什么 NSURL 的 NSURLDocumentIdentifierKey (几乎)总是 nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29752914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com