我的NSDictionaries中的几个数组中有空值,因此我尝试运行它们并删除所有空值。这是我的尝试。
dict = [dict dictionaryByReplacingNullsWithStrings];
NSMutableDictionary *mdict = [dict mutableCopy];
NSArray *clubs = [dict objectForKey:@"clubs"];
NSArray *badges = [dict objectForKey:@"badges"];
NSMutableArray *newBadges = [badges mutableCopy];
NSMutableArray *newClubs = [clubs mutableCopy];
for(int i = 0; i < [clubs count]-1; i++){
NSDictionary *theclub = [clubs objectAtIndex:i];
theclub = [theclub dictionaryByReplacingNullsWithStrings];
newClubs = [newClubs addObject:theclub];
}
for(int i = 0; i < [badges count]-1; i++){
NSDictionary *thebadge = [clubs objectAtIndex:i];
thebadge = [thebadge dictionaryByReplacingNullsWithStrings];
newBadges = [newBadges addObject:thebadge];
}
[mdict removeObjectForKey:@"badges"];
[mdict removeObjectForKey:@"clubs"];
NSLog(@"new Badges %@", newBadges);
[mdict setObject:newBadges forKey:@"badges"];
[mdict setObject:newClubs forKey:@"clubs"];
问题是我在代码中的两行出错:
newClubs = [newClubs addObject:theclub];
和行
newBadges = [newBadges addObject:thebadge];
在两个for循环中。
错误如下:
**Assigning to 'NSMutableArray* __strong' from incompatible type void.**
我正在创建此NSDictionary的JSON提要如下:
{
"id":249,
"email":"email@gmail.com",
"full_name":"Some one",
"statement":"Lets kick some robot but. ",
"avatar_url":"http://www.somesite.com/logo.png",
"clubs":
[{"id":31,"name":"Nintendo Games Club","logo_url":"http://www.somesite.com/logo.png","role_in_club":"Admin","level":{"id":23,"name":"Handicap","position":1,"image_url":"http://www.somesite.com/logo.png","code":"","weeks_to_achieve":1,"times_to_achieve":1,"club_type":{"id":13,"type_name":"Golf","token_criteria":false}}}],
"badges":
[{"id":29,"name":"Nearest the pin","description":"","image_url":"http://www.somesite.com/logo.png","club_id":null},{"id":28,"name":"Longest Drive","description":"","image_url":"http://www.somesite.com/logo.png","club_id":null}]
}
答案 0 :(得分:1)
我修改了你的代码。您已经完成了我在评论中提到的2个错误
dict = [dict dictionaryByReplacingNullsWithStrings];
NSMutableDictionary *mdict = [dict mutableCopy];
NSArray *clubs = [dict objectForKey:@"clubs"];
NSArray *badges = [dict objectForKey:@"badges"];
// You should make fresh arrays as you're going to copy all the memebers again in them.
NSMutableArray *newBadges = [NSMutableArray array];
NSMutableArray *newClubs = [NSMutableArray array];
for(int i = 0; i < [clubs count]-1; i++){
NSDictionary *theclub = [clubs objectAtIndex:i];
theclub = [theclub dictionaryByReplacingNullsWithStrings];
[newClubs addObject:theclub]; // this line adds object in your array and returns void. see documentation
}
for(int i = 0; i < [badges count]-1; i++){
NSDictionary *thebadge = [clubs objectAtIndex:i];
thebadge = [thebadge dictionaryByReplacingNullsWithStrings];
[newBadges addObject:thebadge];
}
[mdict removeObjectForKey:@"badges"];
[mdict removeObjectForKey:@"clubs"];
NSLog(@"new Badges %@", newBadges);
[mdict setObject:newBadges forKey:@"badges"];
[mdict setObject:newClubs forKey:@"clubs"];