获取应用程序数据文件的上次文件修改日期?

ebs*_*bsp 13 iphone xcode cocoa objective-c

我的应用程序数据中有一个plist文件,我想每24小时从网络服务器更新一次.有没有办法检查文件上次修改的时间,或者我应该以某种方式注册更新文件的日期和时间,并使用它来比较?

if (lastMod > currentDate){
    [arrayFromXml writeToFile:path atomically:YES];
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*III 45

您可以使用NSFileManager:

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:@"path/to/my/file" error:nil];

NSDate *date = [attributes fileModificationDate];

// compare 'date'
// date > now
if ([date compareTo:[NSDate date]] == 1) 
{
    [arrayFromXML writeToFile:@"path/to/my/file" atomically:YES];
}
Run Code Online (Sandbox Code Playgroud)

  • @RudolfAdamkovic 它是 NSDictionary 上的 IO 扩展:https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/instm/ NSD 字典/文件修改日期 (2认同)