NSUserDefaults数据保存报错:Attempt to set a non-property-list object...

在使用NSUserDefaults的时候存入数据有时候会报以下错误:

Attempt to set a non-property-list object {
   appkey = 101021;
   authType = 1;
   avatar = "QnYun_pic_1476082244810-1329019310.jpg";
    deviceId = "FC8A1F88-53F6-48F4-B5F6-B18C0B251E8F";
    devices = "53,54,55,56,58,59,60,62,63,64,67,69,74,81,83,98,103,105,111,113,115,116,122,123";
    email = "";
    firstName = vsucvxj;
    lastName = 1233;
    login = GENERAL;
   loginToken = 32766F72FAEB8F5B491B313B9EBE1171D2EB17EFA05D6196BE9D3EA5DDE350FC04A5038C628C8735;
    orgCode = "";
   orgID = A10001394;
    otpDev = "";
   phone = 15010163400;
    serviceToken = "";
    token = 6FC949C9F66E417FCE32C15361DDF1AED2EB17EFA05D6196BE9D3EA5DDE350FC04A5038C628C8735;
   topOrgID = A10001394;
   userCode = "";
    userID = 10004000335;
   userName = purchase01;
} as an NSUserDefaults/CFPreferences value for key userInfoDic

原因:dic 是服务器返回的数据 数据里边有"",而NSUserDefaults是不能被成功解析并存入的,所有在存入之前需要将里边的""改成""即可。

处理从服务返回的NSDictionary值为NSNull或者问题

- (id) processDictionaryIsNSNull:(id)obj{  
    const NSString *blank = @"";  
  
    if ([obj isKindOfClass:[NSDictionary class]]) {  
        NSMutableDictionary *dt = [(NSMutableDictionary*)obj mutableCopy];  
        for(NSString *key in [dt allKeys]) {  
            id object = [dt objectForKey:key];  
            if([object isKindOfClass:[NSNull class]]) {  
                [dt setObject:blank  
                             forKey:key];  
            }  
            else if ([object isKindOfClass:[NSString class]]){  
                NSString *strobj = (NSString*)object;  
                if ([strobj isEqualToString:@""]) {  
                    [dt setObject:blank  
                           forKey:key];  
                }  
            }  
            else if ([object isKindOfClass:[NSArray class]]){  
                NSArray *da = (NSArray*)object;  
                da = [self processDictionaryIsNSNull:da];  
                [dt setObject:da  
                       forKey:key];  
            }  
            else if ([object isKindOfClass:[NSDictionary class]]){  
                NSDictionary *ddc = (NSDictionary*)object;  
                ddc = [self processDictionaryIsNSNull:object];  
                [dt setObject:ddc forKey:key];  
            }  
        }  
        return [dt copy];  
    }  
    else if ([obj isKindOfClass:[NSArray class]]){  
        NSMutableArray *da = [(NSMutableArray*)obj mutableCopy];  
        for (int i=0; i<[da count]; i++) {  
            NSDictionary *dc = [obj objectAtIndex:i];  
            dc = [self processDictionaryIsNSNull:dc];  
            [da replaceObjectAtIndex:i withObject:dc];  
        }  
        return [da copy];  
    }  
    else{  
        return obj;  
    }  
} 

你可能感兴趣的:(NSUserDefaults数据保存报错:Attempt to set a non-property-list object...)