2011-12-26 92 views
2

在可可,你可以得到一个文件的属性如下Cocoa和CoreFoundation返回的文件属性有什么区别?

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

,它将返回键的目录,例如现在

{ 
    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; 
} 

,您还可以使用CoreFoundationMDItem到获取文件属性

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"; 
} 

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

+0

'@“/ path/to/some/file”'是路径,而不是URL。出于这个原因,“'url”是该变量的错误名称。 – 2011-12-26 19:17:35

+0

@Peter明白了,感谢您的指针 – Tony 2011-12-26 20:45:36

回答

5

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

+3

与此相关的一件事是Spotlight可以关闭,无论是按卷还是在系统范围内。如果用户已经关闭了相关的音量和/或一般的音量,那么没有Spotlight索引可以参考,所以元数据API将不会返回任何内容。无论是否启用Spotlight,NSFileManager,核心服务文件管理器以及“stat”和好友都可以工作。 – 2011-12-26 18:56:46

+0

@peter谢谢。这是一个很好的观点。 – 2011-12-26 19:14:09

相关问题