简体   繁体   中英

OSX: how to access to properties in NSFileManager method contentsOfDirectoryAtUrl:

I can't figure out, how to access to properties(I mean file attributes that we ask as NSArray in includingPropertiesForKeys: part of this method) that I mention in NSFileManager method:

-(NSArray *)contentsOfDirectoryAtURL:<#(NSURL *)#> 
          includingPropertiesForKeys:<#(NSArray *)#> 
                             options:<#(NSDirectoryEnumerationOptions)#> 
                               error:<#(NSError *__autoreleasing *)#>

I get a NSArray object contains an array of NSURL objects to files.

So, I can't just get this properties(I just don't know how).

I must use this construction for getting that properties:

NSArray *arrayOfNSURLs = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:myFolderURL
                              includingPropertiesForKeys:@[NSURLContentModificationDateKey, NSURLVolumeIdentifierKey, NSURLLocalizedNameKey,NSURLLocalizedTypeDescriptionKey]
                                                 options:NSDirectoryEnumerationSkipsHiddenFiles
                                                   error:nil];

// I will call all below this 'second part'

id test;
for (id file in arrayOfNSURLs) {
    if ([file isKindOfClass:[NSURL class]]) {
        [file getResourceValue:&test forKey:NSURLContentModificationDateKey error:nil];

        NSLog(@"%@ %@",file ,test);

    }

}

As you can see I must use NSURL method getResourceValue:forKey:error: . But wait a minute, for what I mention this key in NSFileManager method contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: , in includingPropertiesForKeys: part???

I try to put nil as argument for ...includingPropertiesForKeys: part and there is no difference between adding array of keys and nil, "second part" will give you content modification key anyway.

So, my question is simple: why the need of property for keys argument in contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: method? And is there a way to retrieve information mentioned in this keys without second part in my code?

Thanks.

The purpose of the keys in contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: is, according to the docs: "The keys array tells the enumerator object to prefetch and cache information for each item. Prefetching this information improves efficiency by touching the disk only once".

You still have to get those values using getResourceValue:forKey:error:, but the values are now stored in the NSURL object itself, so you don't need to go out to the disk again to get them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM