Cocoa 和 CoreFoundation 返回的文件属性有什么区别?

发布于 2024-12-22 15:35:07 字数 2735 浏览 0 评论 0原文

在Cocoa中,你可以通过如下方式获取文件的属性

NSString *path = @"/path/to/some/file";
NSError *err = ......;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&err];
NSLog(@"%@", dic);

,并且它会返回一个key的目录,例如

{
    NSFileCreationDate = "2009-12-02 10:03:38 +0000";
    NSFileExtensionHidden = 0;
    NSFileGroupOwnerAccountID = 80;
    NSFileGroupOwnerAccountName = admin;
    NSFileHFSCreatorCode = 0;
    NSFileHFSTypeCode = 0;
    NSFileModificationDate = "2009-12-02 19:20:54 +0000";
    NSFileOwnerAccountID = 501;
    NSFileOwnerAccountName = Tony;
    NSFilePosixPermissions = 511;
    NSFileReferenceCount = 1;
    NSFileSize = 496988;
    NSFileSystemFileNumber = 5187496;
    NSFileSystemNumber = 234881029;
    NSFileType = NSFileTypeRegular;
}

现在,你还可以使用CoreFoundation MDItem来获取文件 内容

NSString *path = @"/path/to/some/file";
MDItemRef mdItem = MDItemCreate(kCFAllocatorDefault, (__bridge CFStringRef)path);
CFArrayRef arr = MDItemCopyAttributeNames(mdItem);
CFDictionaryRef dic = MDItemCopyAttributes(mdItem, arr);
NSLog(@"%@", dic);

它将返回类似这样的

{
    kMDItemAuthors =     (
        "Some One"
    );
    kMDItemContentCreationDate = "2009-12-02 10:03:38 +0000";
    kMDItemContentModificationDate = "2009-12-02 19:20:54 +0000";
    kMDItemContentType = "org.openxmlformats.presentationml.presentation";
    kMDItemContentTypeTree =     (
        "org.openxmlformats.presentationml.presentation",
        "org.openxmlformats.openxml",
        "public.zip-archive",
        "com.pkware.zip-archive",
        "public.data",
        "public.item",
        "com.apple.bom-archive",
        "public.archive",
        "public.presentation",
        "public.composite-content",
        "public.content"
    );
    kMDItemDateAdded = "2011-08-16 07:52:53 +0000";
    kMDItemDisplayName = "Some File.pptx";
    kMDItemFSContentChangeDate = "2009-12-02 19:20:54 +0000";
    kMDItemFSCreationDate = "2009-12-02 10:03:38 +0000";
    kMDItemFSCreatorCode = 0;
    kMDItemFSFinderFlags = 0;
    kMDItemFSHasCustomIcon = 0;
    kMDItemFSInvisible = 0;
    kMDItemFSIsExtensionHidden = 0;
    kMDItemFSIsStationery = 0;
    kMDItemFSLabel = 0;
    kMDItemFSName = "Some File.pptx";
    kMDItemFSNodeCount = 496988;
    kMDItemFSOwnerGroupID = 80;
    kMDItemFSOwnerUserID = 501;
    kMDItemFSSize = 496988;
    kMDItemFSTypeCode = 0;
    kMDItemKind = "Microsoft PowerPoint presentation";
    kMDItemLogicalSize = 496988;
    kMDItemPhysicalSize = 499712;
    kMDItemTitle = "PowerPoint Presentation";
}

我的问题是,这两种查找文件属性的方法有什么区别?似乎有些等价,但并非总是等价,哪个更好?为什么会有这两种方式呢?

In Cocoa, you can get the attributes of a file as follow

NSString *path = @"/path/to/some/file";
NSError *err = ......;
NSDictionary *dic = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&err];
NSLog(@"%@", dic);

And it will return a directory of keys, for example

{
    NSFileCreationDate = "2009-12-02 10:03:38 +0000";
    NSFileExtensionHidden = 0;
    NSFileGroupOwnerAccountID = 80;
    NSFileGroupOwnerAccountName = admin;
    NSFileHFSCreatorCode = 0;
    NSFileHFSTypeCode = 0;
    NSFileModificationDate = "2009-12-02 19:20:54 +0000";
    NSFileOwnerAccountID = 501;
    NSFileOwnerAccountName = Tony;
    NSFilePosixPermissions = 511;
    NSFileReferenceCount = 1;
    NSFileSize = 496988;
    NSFileSystemFileNumber = 5187496;
    NSFileSystemNumber = 234881029;
    NSFileType = NSFileTypeRegular;
}

Now, you can also use the CoreFoundation MDItem to get file attributes

NSString *path = @"/path/to/some/file";
MDItemRef mdItem = MDItemCreate(kCFAllocatorDefault, (__bridge CFStringRef)path);
CFArrayRef arr = MDItemCopyAttributeNames(mdItem);
CFDictionaryRef dic = MDItemCopyAttributes(mdItem, arr);
NSLog(@"%@", dic);

And it will return something like this

{
    kMDItemAuthors =     (
        "Some One"
    );
    kMDItemContentCreationDate = "2009-12-02 10:03:38 +0000";
    kMDItemContentModificationDate = "2009-12-02 19:20:54 +0000";
    kMDItemContentType = "org.openxmlformats.presentationml.presentation";
    kMDItemContentTypeTree =     (
        "org.openxmlformats.presentationml.presentation",
        "org.openxmlformats.openxml",
        "public.zip-archive",
        "com.pkware.zip-archive",
        "public.data",
        "public.item",
        "com.apple.bom-archive",
        "public.archive",
        "public.presentation",
        "public.composite-content",
        "public.content"
    );
    kMDItemDateAdded = "2011-08-16 07:52:53 +0000";
    kMDItemDisplayName = "Some File.pptx";
    kMDItemFSContentChangeDate = "2009-12-02 19:20:54 +0000";
    kMDItemFSCreationDate = "2009-12-02 10:03:38 +0000";
    kMDItemFSCreatorCode = 0;
    kMDItemFSFinderFlags = 0;
    kMDItemFSHasCustomIcon = 0;
    kMDItemFSInvisible = 0;
    kMDItemFSIsExtensionHidden = 0;
    kMDItemFSIsStationery = 0;
    kMDItemFSLabel = 0;
    kMDItemFSName = "Some File.pptx";
    kMDItemFSNodeCount = 496988;
    kMDItemFSOwnerGroupID = 80;
    kMDItemFSOwnerUserID = 501;
    kMDItemFSSize = 496988;
    kMDItemFSTypeCode = 0;
    kMDItemKind = "Microsoft PowerPoint presentation";
    kMDItemLogicalSize = 496988;
    kMDItemPhysicalSize = 499712;
    kMDItemTitle = "PowerPoint Presentation";
}

My question is, what's the difference between these two methods of finding file attributes? There seem to be some equivalence but not all the time, which is better? And why are there these two ways anyways?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2024-12-29 15:35:07

从 NSFileManager 返回的属性是存储在文件系统中的文件上的属性。 MDItemCopyAttributes 的结果来自 Spotlight 索引,其中包括大多数(全部?)文件系统属性以及系统上安装的任何 Spotlight 插件的结果。

The attributes returned from NSFileManager are attributes stored on the file in the filesystem. The results from MDItemCopyAttributes are from the Spotlight index which includes most (all?) of the filesystem attributes as well as the results from any of the Spotlight plugins installed on the system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文