Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Cocoa中查找文件的上次修改日期_Cocoa - Fatal编程技术网

在Cocoa中查找文件的上次修改日期

在Cocoa中查找文件的上次修改日期,cocoa,Cocoa,如何在cocoa中找到文件的最后修改日期?请查看NSFileManager的 您感兴趣的关键是NSFileModificationDate。只需更新代码: NSString * path = ... your path here ... NSDate * fileLastModifiedDate = nil; NSError * error = nil; NSDictionary * attrs = [[NSFileManager defaultManager] attributesOfIte

如何在cocoa中找到文件的最后修改日期?

请查看NSFileManager的


您感兴趣的关键是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
    }


}