我正在尝试查找文件的创建日期(不是修改日期)。
虽然修改日期为。
,但创建日期似乎不在文件的属性中我正在使用此代码..
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;
}
这总是返回nil。在调试器中输入'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;
没有创作日期.. bah ..
任何人都知道另一种获取创建日期的方式,还是知道它在iOS中不存在?
答案 0 :(得分:66)
此代码实际上向我返回了良好的创建日期:
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中创建了文件?也许这就是问题所在。
答案 1 :(得分:16)
fileCreationDate
中有一条特殊消息NSDictionary
。以下适用于我:
<强>目标-C:强>
NSDate *date = [attrs fileCreationDate];
<强>夫特:强>
let attrs = try NSFileManager.defaultManager().attributesOfItemAtPath(path) as NSDictionary
attrs.fileCreationDate()
答案 2 :(得分:7)
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)
}
答案 3 :(得分:4)
在Swift 4中,如果你想知道DocumentsDirectory中特定文件的文件创建日期,你可以使用这个方法
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
}
答案 4 :(得分:3)
NSDate *creationDate = nil;
if ([fileManager fileExistsAtPath:filePath]) {
NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
creationDate = attributes[NSFileCreationDate];
}
其here
答案 5 :(得分:2)
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)")
}
答案 6 :(得分:1)
您可以使用fstat64
获取返回结构的st_birthtimespec
成员吗?您需要为该文件创建一个C文件句柄,并将timespec
值转换为NSDate
,但这比没有好。
答案 7 :(得分:1)
Updated answer for Swift 4 to pull out the modified (.modifiedDate
) or creation (.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)
}
答案 8 :(得分:1)
在Swift 5中:
let date = (try? FileManager.default.attributesOfItem(atPath: path))?[.creationDate] as? Date