2009-06-30 82 views

回答

6

退房的NSFileManager

- (NSDictionary *)fileAttributesAtPath:(NSString *)path traverseLink:(BOOL)flag 

你感兴趣的关键是NSFileModificationDate。

+10

这是在10.5中弃用,而不是使用 - (NSDictionary *)attributesOfItemAtPath:(NSString *)路径错误:(NSError **)错误 – aussiegeek 2010-02-12 10:52:39

5

只是为了更新代码:

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]; 
} 
2

这里添加这个答案,因为这是第一个结果,当我搜索了如何做到这一点,但如果你使用迅速,你可能会喜欢这个扩展:

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 
    } 


} 
相关问题