将NSDictionary中的键和值添加到NSMutableArray

时间:2014-10-03 13:10:37

标签: ios objective-c nsmutablearray

我有一个NSMutableArray,其中包含 UserID UserName 键。

我还有NSDictionary个密钥 UserID UserScore

如何将 UserScore 的关键字和值从NSDictionary添加到NSMutableArray,以便用户名用户分数代表相同的 UserID

的NSMutableArray:

UserID = 123,
UserName = JohnP

的NSDictionary:

UserID = 123,
UserScore = 100

所需的输出为NSMutableArray:

UserID = 123,
UserName = JohnP,
UserScore = 100

以下是我使用NSMutableArray将数据检索到Parse然后将数据抓取到标签的方法:

[FBRequestConnection startWithGraphPath:@"/me/friends"
                             parameters:nil
                             HTTPMethod:@"GET"
                      completionHandler:^(
                                          FBRequestConnection *connection,
                                          id result,
                                          NSError *error
                                          ) {
                          FacebookFriends = [result objectForKey:@"data"];
                      }];

NSString *friendname = [[NSString alloc] initWithFormat:@"%@",[[FacebookFriends objectAtIndex:indexPath.row] objectForKey:@"name"]];

2 个答案:

答案 0 :(得分:1)

我认为你真的只想使用一个NSMutableDictionary来存储价值密钥对。

只需指向其键值(objectForKey),即可从其他字典中提供可变字典值。

答案 1 :(得分:0)

在我看来,您希望将两个词典合并为一个词典:

NSDictionary *dict1 = ...;
NSDictionary *dict2 = ...;
NSMutableDictionary *combined = [NSMutableDictionary new];
NSNumber *userid = dict1[@"UserID"];
NSAssert(userid, @"UserID missing from dict1");
NSAssert([userid isEqualToString:dict2[@"UserID"]], @"Mismatched UserIDs");
NSString *username = dict1[@"UserName"];
NSAssert(username, @"Username missing from dict1");
NSNumber *score = dict2[@"UserScore"];
NSAssert(score, @"Score missing from dict2");
combined[@"UserID"] = userid;
combined[@"UserName"] = username;
combined[@"UserScore"] = score;

但是我也怀疑你有一系列词典。如果是这样,请告诉我。