2012-11-27 2 views
1

내 NSDictionaries 내의 몇 가지 배열에 null 값이 있으므로이를 통해 실행하고 모든 null 값을 제거하려고합니다. 여기 내 시도입니다.null 값을 제거하는 NSDictionary. JSON 피드에서 생성

newClubs = [newClubs addObject:theclub]; 

및 루프에 대한이 내 라인

 newBadges = [newBadges addObject:thebadge]; 

모두 :

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 개 라인에 오류가 있다는 것입니다.

{ 
"id":249, 
"email":"[email protected]", 
"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}] 

} 

답변

1

내가 코드를 수정 한 다음대로에서

**Assigning to 'NSMutableArray* __strong' from incompatible type void.** 

json으로 공급 내가이있는 NSDictionary를 작성하고 다음과 같이

오류입니다. 댓글에서 언급 한 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"]; 
+0

감사합니다. :) – jimbob

관련 문제