iOS:如何find文件的创builddate?
我试图find一个文件的创builddate(不修改date)。
创builddate似乎不在文件的属性中,尽pipe修改date是。
我正在使用这个代码
NSFileManager* fm = [NSFileManager defaultManager]; NSString* path = [PathHelpers pathInDocumentsFolderWithFilename:FILE_NAME]; NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil]; if (attrs != nil) { return (NSDate*)[attrs objectForKey: NSFileCreationDate]; } else { return nil; }
这总是返回零。 在debugging器中键入“po attrs”以获取NSDictionary中的键/值对列表返回以下内容..
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = 2010-01-21 11:47:55 +0000;
NSFileOwnerAccountID = 501;
NSFileOwnerAccountName = ben;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
NSFileSize = 338;
NSFileSystemFileNumber = 2234;
NSFileSystemNumber = 42553324;
NSFileType = NSFileTypeRegular;
没有创builddate..呃..
任何人都知道另一种获取创builddate的方式,或者它只是不存在于iOS?
此代码实际上返回给我的良好创builddate:
NSFileManager* fm = [NSFileManager defaultManager]; NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil]; if (attrs != nil) { NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate]; NSLog(@"Date Created: %@", [date description]); } else { NSLog(@"Not found"); }
你在App里面创build文件吗? 也许这是问题所在。
在NSDictionary
有一个特殊的消息fileCreationDate
。 以下为我工作:
Objective-C的:
NSDate *date = [attrs fileCreationDate];
迅速:
let attrs = try NSFileManager.defaultManager().attributesOfItemAtPath(path) as NSDictionary attrs.fileCreationDate()
Swift 2.0版本:
do { let fileAttributes = try NSFileManager.defaultManager().attributesOfItemAtPath(YOURPATH) let creationDate = fileAttributes[NSFileCreationDate] as? NSDate let modificationDate = fileAttributes[NSFileModificationDate] as? NSDate print("creation date of file is", creationDate) print("modification date of file is", modificationDate) }catch let error as NSError { print("file not found:", error) }
NSDate *creationDate = nil; if ([fileManager fileExistsAtPath:filePath]) { NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil]; creationDate = attributes[NSFileCreationDate]; }
它在这里
在Swift 4中,如果您想知道DocumentsDirectory中特定文件的文件创builddate,则可以使用此方法
func getfileCreatedDate(theFile: String) -> Date { var theCreationDate = Date() do{ let aFileAttributes = try FileManager.default.attributesOfItem(atPath: theFile) as [FileAttributeKey:Any] theCreationDate = aFileAttributes[FileAttributeKey.creationDate] as! Date } catch let theError as Error{ print("file not found \(theError)") } return theCreationDate }
你可以使用fstat64
来获得返回结构的st_birthtimespec
成员? 您需要为该文件创build一个C文件句柄,并将timespec
值转换为一个NSDate
,但这总比没有好。
Swift 3版本代码:
do { let fileAttributes = try FileManager.default.attributesOfItem(atPath: yourPathString) let modificationDate = fileAttributes[FileAttributeKey.modificationDate] as! Date print("Modification date: ", modificationDate) } catch let error { print("Error getting file modification attribute date: \(error.localizedDescription)") }
更新了Swift 4的答案以提取修改的( .modifiedDate
)或创build( .creationDate
)date:
let file: URL = ... if let attributes = try? FileManager.default.attributesOfItem(atPath: file.path) as [FileAttributeKey: Any], let creationDate = attributes[FileAttributeKey.creationDate] as? Date { print(creationDate) }
- 使用预先通过URL提供的文件,它将请求其属性。 如果成功返回[FileAttributeKey:Any]的字典
- 使用步骤1中的字典,然后取出创builddate(如果您愿意,也可以修改),并使用条件解包,如果成功则将其分配给date
- 假设前两步是成功的,你现在有一个你可以使用的date