Advertisements
NSDirectoryEnumerator by example
Getting File and Directory Attributes
- directoryAttributes
Returns an NSDictionary object that contains the attributes of the directory at which enumeration started.
Example
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory]; NSLog(@"%@",[directoryEnumerator directoryAttributes]);
Output
2014-04-12 05:09:26.803 iOS-Tutorial[1358:a0b] { NSFileCreationDate = "2014-03-22 01:41:08 +0000"; NSFileExtensionHidden = 0; NSFileGroupOwnerAccountID = 20; NSFileGroupOwnerAccountName = staff; NSFileModificationDate = "2014-04-02 15:10:18 +0000"; NSFileOwnerAccountID = 501; NSFilePosixPermissions = 493; NSFileReferenceCount = 5; NSFileSize = 170; NSFileSystemFileNumber = 3576058; NSFileSystemNumber = 16777217; NSFileType = NSFileTypeDirectory; }
- fileAttributes
Returns an object that contains the attributes of the most recently returned file or subdirectory (as referenced by the pathname).
Example
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory]; NSString *pathName; while (pathName = [directoryEnumerator nextObject]) { NSLog(@"%@",[directoryEnumerator fileAttributes]); break; }
Output
2014-04-12 05:14:05.880 iOS-Tutorial[1416:a0b] { NSFileCreationDate = "2014-03-22 09:14:04 +0000"; NSFileExtensionHidden = 1; NSFileGroupOwnerAccountID = 20; NSFileGroupOwnerAccountName = staff; NSFileModificationDate = "2014-03-22 09:14:04 +0000"; NSFileOwnerAccountID = 501; NSFilePosixPermissions = 420; NSFileReferenceCount = 1; NSFileSize = 6148; NSFileSystemFileNumber = 3587553; NSFileSystemNumber = 16777217; NSFileType = NSFileTypeRegular; }
- level
Returns the number of levels deep the current object is in the directory hierarchy being enumerated.
Example
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory]; while ([directoryEnumerator nextObject]) { NSLog(@"%d",[directoryEnumerator level]); break; }
Output
2014-04-12 05:15:34.706 iOS-Tutorial[1441:a0b] 1
Skipping Subdirectories
- skipDescendents
Causes the receiver to skip recursion into the most recently obtained subdirectory.
Example
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory]; [directoryEnumerator skipDescendents]; NSString *path; while (path = [directoryEnumerator nextObject]) { NSLog(@"%@",path); }
Output
2014-04-12 05:16:56.944 iOS-Tutorial[1467:a0b] .DS_Store 2014-04-12 05:16:56.945 iOS-Tutorial[1467:a0b] myDict.plist 2014-04-12 05:16:56.946 iOS-Tutorial[1467:a0b] sample-string.txt
Advertisements