大家好,又见面了,我是全栈君。
在使用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 = "<null>";
firstName = vsucvxj;
lastName = 1233;
login = GENERAL;
loginToken = 32766F72FAEB8F5B491B313B9EBE1171D2EB17EFA05D6196BE9D3EA5DDE350FC04A5038C628C8735;
orgCode = "";
orgID = A10001394;
otpDev = "<null>";
phone = 15010163400;
serviceToken = "<null>";
token = 6FC949C9F66E417FCE32C15361DDF1AED2EB17EFA05D6196BE9D3EA5DDE350FC04A5038C628C8735;
topOrgID = A10001394;
userCode = "<null>";
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:@"<null>"]) {
[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;
}
}
复制代码
转载于:https://juejin.im/post/5a31006551882527541050aa
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/107833.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...