如果文件超过24小时,我试图自动更新plist文件,但我无法弄清楚如何比较这两个日期。
// get last modification date
NSString *dir = path;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:dir error:nil];
NSDate *date = [attributes fileModificationDate];
NSDate *fileModPlus24Hours = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:date];
思考类似的事情:
if (date > fileModPlus24Hours) {
// update file
}
有什么建议吗?
答案 0 :(得分:0)
if ([date compare:fileModPlus24Hours] == NSOrderedDescending) {
// update file
}
比较结果可以是NSOrderedAscending
,NSOrderedDescending
或NSOrderedSame
。
我不认为您的代码可以通过将date
与fileModPlus24Hours
进行比较来实现。您应该将当前日期与fileModPlus24Hours
进行比较:
NSDate *now = [NSDate date];
if ([now compare:fileModPlus24Hours] == NSOrderedDescending) {
// update file
}