Retrieving files (and its attributes) in a directory

I want to get a list of files in a directory, but I need to know if the file is a directory when it was last modified and canceled its name :). So I tried this:

NSFileManager *fm = [NSFileManager defaultManager]; NSURL *directoryURL = [NSURL fileURLWithPath:@"/Users/nacho4d/Desktop/"]; NSArray *urls = [fm contentsOfDirectoryAtURL:directoryURL includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, NSURLIsDirectoryKey, NSURLContentModificationDateKey, nil] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil]; NSError *error = nil; NSString *name; NSDate *modificationDate; NSNumber *isDirectory; for (NSURL *url in urls) { if (![url getResourceValue:&name forKey:NSURLNameKey error:&error]) { NSLog(@"name: %@", [error localizedDescription]);error = nil; } if (![url getResourceValue:&modificationDate forKey:NSURLContentModificationDateKey error:&error]) { NSLog(@"modificationDate: %@", [error localizedDescription]); error = nil; } if (![url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) { NSLog(@"isDirectory: %@", [error localizedDescription]); error = nil; } NSLog(@"%@ %@ %@", name, modificationDate, isDirectory); } 

However, I always get (null) (null) (null) , no errors, but no data; (

I wonder what is wrong here?

PS: I know the contentsOfDirectoryAtPath:error: and attributesOfItemAtPath:error: but I think this is not the best way to do this, is it? I plan to use this in folders that can contain a large number of files, so I would like to do this in the most efficient way.

Change for iOS5 and later

As @jeremyP pointed out, According to the docs, the method is not implemented in iOS4, but since iOS5 and later work fine:

Availability
Available in iOS 5.0 and later. (The symbol is present in iOS 4, but does not perform any operations.)

This question was made when iOS4 was the last, I think. Now in iOS 5 and 6 Everything works as expected :)

+6
source share
1 answer

This is not the answer to your question, but your error check should look like this:

 if (![url getResourceValue:&name forKey:NSURLNameKey error:&error]) { NSLog(@"name: %@", [error localizedDescription]);error = nil; } 

Edit

It was not possible to find out what the problem was, but just re-read the documentation using the method . An important suggestion is a discussion that states:

This method is not implemented in iOS, so it does not perform any operations.

+7
source

Source: https://habr.com/ru/post/892041/


All Articles