使用NSUserDefaults进行数据缓存时遇到的问题

从服务器获取到字典类型数据,用NSUserDefaults缓存到本地

 

初始的- (void)dataLoad {

     NSMutableDictionary * aaa = [originalData valueForKey:@"userInfo"];

        [userDefaults setValue:aaa forKey:@"data"];

        [userDefaults synchronize];

 }

 

由于数据中有CFNULL 调用下面方法,进行替换

- (void)cleanDictionary:(NSMutableDictionary *)dictionary {

    [dictionary enumerateKeysAndObjectsUsingBlock:^( id key, id obj, BOOL *stop){

        if(obj == [NSNull null]) {

            [dictionary setObject:@"" forKey:key];

        } else if ([obj isKindOfClass:[NSDictionary class]]) {

            [self cleanDictionary:obj];

        }

        NSLog(@"key:%@,,value:%@",key,obj);

    }];

}//属性列表中不能存有CFNULL,遍历进行替换

然而接收的数据无法进行数据的修改,我们就需要MutableCopy来解决

修改的- (void)dataLoad {

        NSMutableDictionary * aaa = [[originalData valueForKey:@"userInfo"] mutableCopy];

    [self cleanDictionary:aaa];

        [userDefaults setValue:aaa forKey:@"data"];

        [userDefaults synchronize];

 }

这样就好了       

目前还是个新手,对于mutableCopy和copy还不是很了解

posted @ 2015-08-28 11:29  咔咔桑丶  阅读(264)  评论(0)    收藏  举报