从NSMutableArray创建NSMUtableDictionary

时间:2013-04-22 07:46:26

标签: ios objective-c nsmutablearray nsmutabledictionary

Web服务调用返回JSON响应,我将其放入NSMutableArray。 JSON响应如下所示,

 (
        {
        DispName = "Jonny Depp (Marvel Comics)";
        "Int_Adr" = 273;
        "Int_Group" = 0;
    },
        {
        DispName = "Mahendra Singh Dhoni (Indian Premier League)";
        "Int_Adr" = 265;
        "Int_Group" = 0;
    },
        {
        DispName = "Otara De Mel (ODEL UNLIMITED)";
        "Int_Adr" = 496;
        "Int_Group" = 0;
    },
        {
        DispName = "Rahul Dravid (Indian Premier League)";
        "Int_Adr" = 266;
        "Int_Group" = 0;
    }
)

现在我想创建另一个只包含 DispName Int_Adr 键的NSMutableDictionary

所以我正在做这样的事情。我已经在.h文件中声明了NSMutableDictionary

@property (nonatomic, strong) NSMutableDictionary *recipients;

在.m文件中,

// get the JSON response and put it in an array
NSMutableArray *contactsArray = [jsonParser objectWithString:response];
NSLog(@"%@, array count = %d", contactsArray, contactsArray.count); // the count is 50

//[self.recipients removeAllObjects];
for (NSDictionary *item in contactsArray) {
    [self.recipients setObject:[item objectForKey:@"Int_Adr"] forKey:@"Int_Adr"];
    [self.recipients setObject:[item objectForKey:@"DispName"] forKey:@"DispName"];
}
NSLog(@"%@, array count = %d", self.recipients, self.recipients.count); // the count shows 2!

我只将这两个值设置为收件人字典。但是当我记录输出时,它只显示最后一组。

{
    DispName = "Rahul Dravid (Indian Premier League)";
    "Int_Adr" = 266;
}

我该如何解决这个问题?

谢谢。

5 个答案:

答案 0 :(得分:2)

这是我的建议,将NSMutableArrayNSMutableDictionary一起使用。 JSON表示它是字典数组。你必须重新设计你的代码。

@property (nonatomic, strong) NSMutableArray *recipients; //not NSMutableDictionary

//Store
for (NSDictionary *item in contactsArray) {
    NSMutableDictionary *yourDict=[[NSMutableDictionary alloc] init];
    [yourDict setObject:[item objectForKey:@"Int_Adr"] forKey:@"Int_Adr"];
    [yourDict setObject:[item objectForKey:@"DispName"] forKey:@"DispName"];

    [self.recipients addObject:yourDict];
}

//Access
for (NSDictionary *item in self.recipients) {
    NSString *address = [item objectForKey:@"Int_Adr"];
    NSString *displayName = [item objectForKey:@"DispName"]
}

答案 1 :(得分:0)

原因是每次更新字典中的值时,这就是为什么最后的值才会出现。为了达到你的要求,需要为不同的值使用不同的键或者将新的字典添加到另一个数组。

答案 2 :(得分:0)

字典有键和值。键是唯一的,这意味着如果你这样做:

my_dict["a"] = "hello";
my_dict["b"] = "goodbye";

整本字典是:

"a" => "hello"
"b" => "goodbye"

然后,如果你这样做:

my_dict["a"] = "red";
my_dict["b"] = "blue";

整本字典是:

"a" => "red"
"b" => "blue"

换句话说,对应于“a”键的值被“红色”覆盖。

答案 3 :(得分:0)

非常简单,见下文

for (NSDictionary *item in contactsArray) {
    NSMutableDictionary *newDic=[[NSMutableDictionary alloc] init];
    [newDic setObject:[item objectForKey:@"Int_Adr"] forKey:@"Int_Adr"];
    [newDic setObject:[item objectForKey:@"DispName"] forKey:@"DispName"];

    [self.recipients addObject:newDic forKey:@"data"];
}

您需要初始化新的数据集以包含特定数据,在您的代码中,您只是更新并最终更新,最后一个更新,而您只获得最后一个。

答案 4 :(得分:0)

要将NSMutableArray转换为NSMutableDictionary,您需要为NSMutableArray的每个对象提供一个键,如下所示。

- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
{
    id objectInstance;
    NSUInteger indexKey = 0;

    NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
    for (objectInstance in array)
        [mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]];

    return (NSDictionary *)mutableDictionary;
}

此处将您的数组作为此indexKeyedDictionaryFromArray:方法中的参数。它会返回一本字典。

希望它对你有所帮助。