将具有相同键的所有对象从 NSDictionary 添加到 NSArray
NSArray *myArray=[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
for(NSDictionary *d in myArray){
newInfo=[d valueForKey:@"screen_name"];
NSLog(@"%@", newInfo);
listOfTerms=[[NSMutableArray alloc]initWithObjects:newInfo, nil];
NSLog(@"here %@", listOfTerms);
}
上面你可以看到我正在使用的代码,但 listOfTerms 只有一个条目,它似乎是字典中的最后一个对象。我已经尝试解决这个问题几个小时了。任何帮助将不胜感激。
NSArray *myArray=[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
for(NSDictionary *d in myArray){
newInfo=[d valueForKey:@"screen_name"];
NSLog(@"%@", newInfo);
listOfTerms=[[NSMutableArray alloc]initWithObjects:newInfo, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用键值编码,整个代码可以替换为:
了解框架,利用它来发挥你的优势。
Using Key-Value Coding, that whole code can be replaced by:
Know the framework, use it for your advantage.
您在每次迭代中都会覆盖 listOfTerms,请尝试以下操作:
You are overwriting listOfTerms in every iteration, try this instead: