Dima Deplov
Dima Deplov

Reputation: 3718

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.

Upvotes: 0

Views: 837

Answers (1)

rdelmar
rdelmar

Reputation: 104092

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.

Upvotes: 1

Related Questions