如何根据细节制作Mutable Dictionary以及如何在Mutable Array(iphone)中添加该字典

时间:2011-08-26 09:21:57

标签: iphone nsmutablearray nsmutabledictionary

我有一些像 -

这样的细节

for userone -

name-user1
city-new york

name-user2
city-oslo

现在,我想使用键namelocation

在字典中添加这些详细信息

我想创建一个字典数组

谁能告诉我该怎么做?

2 个答案:

答案 0 :(得分:3)

NSArray* nameArray = [NSArray arrayWithObjects:@"abc",@"asdfds",@"sdffasd",@"sadfds",@"asdfsd",nil];
NSArray* cityArray = [NSArray arrayWithObjects:@"abasdasc",@"asadws",@"asd",@"sads",@"fsd",nil];

NSMutableArray* dictArray = [NSMutableArray arrayWithCapacity:0];

for(int i = 0; i < [nameArray count]; i++)
{
    NSMutableDictionary* dictDetails = [[NSMutableDictionary dictionaryWithCapacity:0]autorelease];
    [dictDetails setValue:[nameArray objectAtIndex:i] forKey:@"Name"];
    [dictDetails setValue:[cityArray objectAtIndex:i] forKey:@"City"];

    [dictArray addObject:dictDetails];
}

NSLog(@"array is %@",dictArray);
编辑:因为我向dictDetails发送了一个发布方法,所以它崩溃了。而是将其保留在自动释放中。

答案 1 :(得分:1)

NSDictionary* dict1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"user1", @"new york", nil] forKeys:[NSArray arrayWithObjects:@"name", @"location", nil]];
NSDictionary* dict2 =[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"user2", @"oslo", nil] forKeys:[NSArray arrayWithObjects:@"name", @"location", nil]]; 
NSArray* dictsArray = [NSArray arrayWithObjects:dict1, dict2, nil];