获取iOS中文件系统目录中存储对象的所有属性

时间:2013-03-09 19:32:42

标签: ios objective-c persistence

我知道这个方法:resourceValueForKeys给出一个NSURL和一组键将为你获取文件的属性...但我需要的是枚举的东西 通过给定NSURL目录中的所有文件,并为我提供属性。

我尝试使用NSFileSystem类中的枚举器执行此操作,但无法使其工作,有人知道正确的方法吗?...我没有使用Core Data。

   NSDictionary *values = [fileDirectory resourceValuesForKeys:@[NSURLAttributeModificationDateKey] error:nil];
    NSLog(@"object: %@", values);

2 个答案:

答案 0 :(得分:4)

你走了:

NSString *path = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/your file path"];
NSLog(@"Your path: %@",path);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSDictionary *attributesOfYourItem = [fileManager attributesOfItemAtPath:path error:&error];
if(error)
    NSLog(@"Error: %@",error);
else
    NSLog(@"Attribute dictionary: %@",attributesOfYourItem);

答案 1 :(得分:2)

枚举给定目录中的所有文件:

NSArray * arrayOfURLs = [fileManager contentsOfDirectoryAtURL:URL includingPropertiesForKeys:@[NSURLIsDirectoryKey,
        NSURLNameKey,NSURLFileSizeKey,NSURLContentModificationDateKey,
        NSURLLocalizedTypeDescriptionKey]
                              options:NSDirectoryEnumerationSkipsHiddenFiles 
                                error:nil];

for ( NSURL file in arrayOfURLs ) {
    NSString * nameOfFile;
    [file getResourceValue:&nameOfFile forKey:NSURLNameKey error:nil];
    NSLog(@"%@", nameOfFile);

    ...... and so forth for the rest of values ...

}

这样您就可以获得该文件夹中包含的所有文件的所有值。