开发者

How to make Mutabledictionary from details and how to add that dictionary in MutableArray (iphone)

开发者 https://www.devze.com 2023-03-30 18:38 出处:网络
I have some de开发者_开发技巧tails like-- for userone- name-user1 city-new york name-user2 city-oslo

I have some de开发者_开发技巧tails like--

for userone-

name-user1

city-new york

name-user2

city-oslo

Now I want to add these details in a dictionary using key name and location

and I want to make an array of that dictionary

can anyone tell me what to do?


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);

EDIT: it was crashing before as i had sent a release method to dictDetails. Instead keep it in autorelease only.


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消