How to make Mutabledictionary from details and how to add that dictionary in MutableArray (iphone)
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];
精彩评论