获取目录详细信息的API是什么?
答
希望你在这里找到答案:
一个文件或目录的属性可以使用 attributesOfItemAtPath方法获得。这需要参数 目录的路径和一个可选的NSError对象,其中将放置关于 任何错误的信息(如果不需要此信息,可将其指定为NULL)。结果以 的形式返回NSDictionary字典对象(有关使用 字典对象参考Objective-C字典对象的详细信息)。
本字典一些键:从上述文章
NSFileType
NSFileTypeDirectory
NSFileTypeBlockSpecial
NSFileTypeUnknown
NSFileSize
NSFileModificationDate
NSFileOwnerAccountName
NSFileGroupOwnerAccountName
NSFilePosixPermissions
NSFileSystemNumber
NSFileCreationDate
NSFileOwnerAccountID
NSFileGroupOwnerAccountID
实施例:
(我们可使用提取/ tmp目录的创建日期,文件类型和POSIX权限下面的代码摘录)
NSFileManager *filemgr;
NSDictionary *attribs;
filemgr = [NSFileManager defaultManager];
attribs = [filemgr attributesOfItemAtPath: @"/tmp" error: NULL];
NSLog (@"Created on %@", [attribs objectForKey: NSFileCreationDate]);
NSLog (@"File type %@", [attribs objectForKey: NSFileType]);
NSLog (@"POSIX Permissions %@", [attribs objectForKey: NSFilePosixPermissions]);
答
最前沿(may.13.2012)使用CFURL/NSURL属性:
id outName = nil;
NSString * key = NSURLNameKey;
NSError * outError = nil;
if (YES == [url getResourceValue:&value forKey:key error:&outError]) {
NSString * name = outName;
...
}
注意:确保你只需要什么要求,并注意有一批请求方法/功能,以尽量减少多余的请求。如果使用NSURL,您可以使用:
NSArray * keys = ...;
NSError * outError = nil;
NSDictionary * properties = [url resourceValuesForKeys:keys error:&outError];