NSFileManager不从NSFileExtendedAttributes读取kMDItemWhereFroms

Ben*_*hen 2 cocoa objective-c nsfilemanager

对于我正在运行的名为SourceControl的小程序,我正在尝试以下方法:在计算机上的某个(现在是静态的)路径中读取一个文件(从Internet下载).

主要思想是从下载文件的位置获取源代码.

像这样:

文件来源(图片在这里)

我已经尝试了一些代码,但我得到的是一个指向另一个对象的指针,这不是我真正喜欢的...我想显示实际的字符串,如:" http://www.sunjets.be/ _images/boekingsengine/grevas1_nl.jpg "

得到NSFileExtendedAttributes最终得到的com.apple.metadata:kMDItemWhereFroms:

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        //Static path to my file/image
        NSString* myPath = @"/Users/Verbe/Klassieke_Oudheid.JPG";

        //creating the filemanager
        NSFileManager *filemgr = [[NSFileManager alloc]init];

        //if the path exists, whiii it exists!
        if ([filemgr fileExistsAtPath:myPath])
        {
            NSLog(@"file exists!");

            //Set the fileattributes to the dictionary!
            NSDictionary *fileAttributes = [filemgr attributesOfItemAtPath:myPath error:
                                            nil];

            for (NSString* myKey in fileAttributes)
            {
                if ([myKey isEqualToString:@"NSFileExtendedAttributes"])
                {
                    NSLog(@"MyKey = %@ with attribute: %@",myKey, [fileAttributes objectForKey:myKey]);

                }
            }

        }
        else
        {
            NSLog(@"File does not exist!");
        }

}
    return 0;
Run Code Online (Sandbox Code Playgroud)

结果如下:

2013-10-11 04:23:32.167 CustomInit [1016:303]文件存在!2013-10-11 04:23:32.190 CustomInit [1016:303] MyKey =带有属性的NSFileExtendedAttributes:{"com.apple.metadata:kMDItemDownloadedDate"= <62706c69 73743030 a1013341 b8078c4f a88e8a08 0a000000 00000001 01000000 00000000 02000000 00000000 00000000 00000000 13>; "com.apple.metadata:kMDItemWhereFroms"= <62706c69 73743030 a1015f10 3b687474 703a2f2f 7777772e 73756e6a 6574732e 62652f5f 696d6167 65732f62 6f656b69 6e677365 6e67696e 652f6772 65766173 315f6e6c 2e6a7067 080a0000 00000000 01010000 00000000 00020000 00000000 00000000 00000000 0048>; "com.apple.quarantine"= <30​​303030 3b353235 37353463 663b5361 66617269 3b>; 程序以退出代码结束:0

请注意,com.apple.metadata:kMDItemDownloadedDate并且com.apple.metadata:kMDItemWhereFroms实际上在那里!

这里发生了什么,我该如何解决?

Pet*_*sey 5

这些是元数据(Spotlight)属性,因此您打算使用元数据框架.使用文件的URL创建MDItem,询问该项目的属性值.

像这样:

MDItemRef item = MDItemCreateWithURL(kCFAllocatorDefault, myFileURL);
NSArray *whereFroms = CFBridgingRelease(MDItemCopyAttribute(item, kMDItemWhereFroms));
Run Code Online (Sandbox Code Playgroud)

不需要plist解码.