在Cocoa中查找文件的上次修改日期
如何在cocoa中找到文件的最后修改日期?请查看NSFileManager的在Cocoa中查找文件的上次修改日期,cocoa,Cocoa,如何在cocoa中找到文件的最后修改日期?请查看NSFileManager的 您感兴趣的关键是NSFileModificationDate。只需更新代码: NSString * path = ... your path here ... NSDate * fileLastModifiedDate = nil; NSError * error = nil; NSDictionary * attrs = [[NSFileManager defaultManager] attributesOfIte
您感兴趣的关键是NSFileModificationDate。只需更新代码:
NSString * path = ... your path here ...
NSDate * fileLastModifiedDate = nil;
NSError * error = nil;
NSDictionary * attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];
if (attrs && !error)
{
fileLastModifiedDate = [attrs fileModificationDate];
}
在此处添加此答案,因为这是我搜索如何执行此操作时的第一个结果,但如果您使用swift,您可能会喜欢此扩展:
extension NSFileManager {
func modificationDateForFileAtPath(path:String) -> NSDate? {
guard let attributes = try? self.attributesOfItemAtPath(path) else { return nil }
return attributes[NSFileModificationDate] as? NSDate
}
func creationDateForFileAtPath(path:String) -> NSDate? {
guard let attributes = try? self.attributesOfItemAtPath(path) else { return nil }
return attributes[NSFileCreationDate] as? NSDate
}
}
这在10.5中被弃用,而是使用-(NSDictionary*)AttributesFiteMatPath:(NSString*)路径错误:(NSError**)错误
extension NSFileManager {
func modificationDateForFileAtPath(path:String) -> NSDate? {
guard let attributes = try? self.attributesOfItemAtPath(path) else { return nil }
return attributes[NSFileModificationDate] as? NSDate
}
func creationDateForFileAtPath(path:String) -> NSDate? {
guard let attributes = try? self.attributesOfItemAtPath(path) else { return nil }
return attributes[NSFileCreationDate] as? NSDate
}
}