1

我的 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}]

}
4

1 回答 1

1

我已经修改了你的代码。你犯了两个我在评论中提到的错误

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"];
于 2012-11-30T11:13:43.277 回答