IOS----plist和date类型的冲突

来源:互联网 发布:java socket服务端程序 编辑:程序博客网 时间:2024/06/01 09:20

(ps,制作记录,如果错误了希望大家指正)

我发现虽然支持下面几种类型,但是蛮多都是手动填入的

比如我今天的写了一个方法,原本想删除里面的一个对象,可是结果大跌眼镜

-(int)remove:(Note *)model{    NSString *path = [self applicationDocumentsDirectoryFile];        NSMutableArray *ma = [[NSMutableArray alloc]initWithContentsOfFile:path];        NSLog(@"---存入的达特:%@",model.date);        for(NSDictionary *dic in ma)    {        NSDate *myDate = [dic objectForKey:@"date"];                if([model.date isEqualToDate:myDate])        {            [ma removeObject:dic];                        NSLog(@"Succeed");                        break;        }    }        [ma writeToFile:path atomically:YES];    return 0;    }
我存入的数据的方法为,直接把Date类型存入,但是结果
-(int)create:(Note *)model{    NSString *path = [self applicationDocumentsDirectoryFile];        NSMutableArray *ma = [[NSMutableArray alloc]initWithContentsOfFile:path];            NSDictionary *ndc = [[NSDictionary alloc]initWithObjects:@[model.content,model.date] forKeys:@[@"content",@"date"]];        [ma addObject:ndc];            [ma writeToFile:path atomically:YES];    return 0;}

单元测试方法如下:

-(void)testCreate{    NoteDao *dao =  [NoteDao sharedManager];        NSDateFormatter *formater = [[NSDateFormatter alloc]init];    [formater setDateFormat:@"YYYY-MM-dd HH:mm:ss"];    NSDate *date =  [formater dateFromString:@"2010-11-23 12:23:10"];        Note *note = [[Note alloc]initWithContent:@"帅哥美女么么哒" andDate:date];        [dao create:note];        NSString *str = [dao applicationDocumentsDirectoryFile];        NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:str];            NSLog(@"%@",array);            XCTAssertEqual(array.count, 2, @"just a test");}

本来希望存入的数据为:
2010-11-23 12:23:10
可是存进去的数据为:

date = "2010-11-23 04:23:10 +0000";

结果就不一样了,所以我觉得Date类型数据的写入还不如先写NSString来的方便,然后再转成Date类型的


不知道有没有高手知道这是为什么,因为是新手所以疑惑比较多~~ 请大家指教

0 0
原创粉丝点击