我有一个非常简单的代码来获取iPhone中我的CacheFolder中存储的所有文件的列表
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory
NSString *documentsPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:APP_NAME];
NSError *error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];
但是,我想要获取每个文件大小和创建日期,而不仅仅是名称。这有可能吗?
答案 0 :(得分:1)
NSError *attributesError;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:documentsPath error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
NSDate *creationDate = (NSDate *)[fileAttributes objectForKey:NSFileCreationDate];
您还可以获得以下属性:
NSString * const NSFileType;
NSString * const NSFileSize;
NSString * const NSFileModificationDate;
NSString * const NSFileReferenceCount;
NSString * const NSFileDeviceIdentifier;
NSString * const NSFileOwnerAccountName;
NSString * const NSFileGroupOwnerAccountName;
NSString * const NSFilePosixPermissions ;
NSString * const NSFileSystemNumber;
NSString * const NSFileSystemFileNumber;
NSString * const NSFileExtensionHidden;
NSString * const NSFileHFSCreatorCode;
NSString * const NSFileHFSTypeCode;
NSString * const NSFileImmutable;
NSString * const NSFileAppendOnly;
NSString * const NSFileCreationDate;
NSString * const NSFileOwnerAccountID;
NSString * const NSFileGroupOwnerAccountID;
NSString * const NSFileBusy;
答案 1 :(得分:0)
你想要的是NSFileManager's attributesOfItemAtPath:error
API,它将返回一个包含NSFileSize
and NSFileCreationDate
等属性的字典。