删除空值的 NSDictionary.从 JSON 源创建



我的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"];

问题是我在代码中的 2 行上收到错误:

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

我已经修改了你的代码。你犯了我在评论中提到的 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"];

最新更新