iOS上缓存文件有效期的实现

来源:互联网 发布:2017中国贫富差距数据 编辑:程序博客网 时间:2024/05/29 13:21
@implementation NSString (NSString_PathValid)- (int)Interval{NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self error:nil];//NSLog(@"create date:%@",[attributes fileModificationDate]);NSString *dateString = [NSString stringWithFormat:@"%@",[attributes fileModificationDate]];NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];NSDate *formatterDate = [inputFormatter dateFromString:dateString];unsigned int unitFlags = NSDayCalendarUnit;    NSCalendar *cal = [NSCalendar currentCalendar];    NSDateComponents *d = [cal components:unitFlags fromDate:formatterDate toDate:[NSDate date] options:0];[inputFormatter release];//NSLog(@"%d,%d,%d,%d",[d year],[d day],[d hour],[d minute]);int result = [d day];    //return 0;return result;}- (BOOL)isValid{if ([self Interval] < VALIDDAYS) { //VALIDDAYS = 有效时间天数return YES;}return NO;}@endNSString *path = <filePath>;if ([path isValid]){// 有效}else{// 无效}

原创粉丝点击