add all objects with the same key from an NSDictionary to a NSArray
NSArray *myArray=[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
for(NSDictionary *d in myArray){
newInfo=[d valueForKey:@"screen_name"];
NSLog(@"%@", newInfo);
listOfTerms=[[NSMutableArray alloc]initWithObjects:newI开发者_如何学Pythonnfo, nil];
NSLog(@"here %@", listOfTerms);
}
above you can see the code i'm using, but listOfTerms only ever has one entry which seems to be the last object in the dictionary. i've been trying to sort this out for some hours now. any help would be appreciated.
Using Key-Value Coding, that whole code can be replaced by:
NSArray *myArray=[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
NSArray *listOfTerms = [myArray valueForKey:@"screen_name"];
Know the framework, use it for your advantage.
You are overwriting listOfTerms in every iteration, try this instead:
listOfTerms = [[NSMutableArray alloc] init];
NSArray *myArray=[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
for(NSDictionary *d in myArray){
newInfo=[d valueForKey:@"screen_name"];
NSLog(@"%@", newInfo);
[listOfTerms addObject:newInfo];
NSLog(@"here %@", listOfTerms);
}
精彩评论