Ben*_*ton 34 date file nsfilemanager ios
我正在尝试查找文件的创建日期(不是修改日期).
虽然修改日期是,但创建日期似乎不在文件的属性中.
我正在使用此代码..
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;
}
Run Code Online (Sandbox Code Playgroud)
这总是返回零.在调试器中键入"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;
没有创作日期..呸..
任何人都知道另一种获取创建日期的方式,还是只是在iOS中不存在?
Ori*_*eto 67
这段代码实际上给我留下了良好的创建日期:
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");
}
Run Code Online (Sandbox Code Playgroud)
您是否在App中创建文件?也许这就是问题所在.
Rid*_*lly 17
有一个特殊的消息fileCreationDate对于在NSDictionary.以下适用于我:
Objective-C的:
NSDate *date = [attrs fileCreationDate];
Run Code Online (Sandbox Code Playgroud)
迅速:
let attrs = try NSFileManager.defaultManager().attributesOfItemAtPath(path) as NSDictionary
attrs.fileCreationDate()
Run Code Online (Sandbox Code Playgroud)
更新Swift 4 的答案以提取修改 ( .modifiedDate) 或创建 ( .creationDate) 日期:
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)
}
Run Code Online (Sandbox Code Playgroud)
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)
}
Run Code Online (Sandbox Code Playgroud)
在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
}
Run Code Online (Sandbox Code Playgroud)
在 Swift 5 中:
let date = (try? FileManager.default.attributesOfItem(atPath: path))?[.creationDate] as? Date
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33907 次 |
| 最近记录: |