2009-06-30 81 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 
    } 


} 
相關問題