xcode 获取应用程序数据文件的最后文件修改日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9365355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get last file modification date of application data files?
提问by ebsp
I have a plist file in my application data that i want to update from a webserver every 24 hours. Is there a way to check when the file is last modified or should i, in some way register the date and time when i update the file, and use that to compare to?
我的应用程序数据中有一个 plist 文件,我想每 24 小时从网络服务器更新一次。有没有办法检查文件上次修改的时间,或者我应该以某种方式注册更新文件的日期和时间,并使用它进行比较?
if (lastMod > currentDate){
[arrayFromXml writeToFile:path atomically:YES];
}
回答by Richard J. Ross III
You can use NSFileManager for this:
您可以为此使用 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];
}
回答by Shubhank
you can store the NSDate
in NSUserDefaults
when saving..and then compare it with current time to check the difference,,
您可以NSDate
在保存NSUserDefaults
时将其存储..然后将其与当前时间进行比较以检查差异,,