我有一些像 -
这样的细节for userone -
name-user1
city-new york
name-user2
city-oslo
现在,我想使用键name
和location
我想创建一个字典数组
谁能告诉我该怎么做?
答案 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];