Thursday, May 9, 2013

NSDictionary initWithObjects example ios


initWithObjects :forKeys:

Initializes a newly allocated dictionary with entries constructed from the contents of the objects and keys arrays.
- (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys
Parameters
objects
An array containing the values for the new dictionary.
keys
An array containing the keys for the new dictionary. Each key is copied (using copyWithZone:; keys must conform to the NSCopying protocol), and the copy is added to the new dictionary.
Discussion of [NSDictionary initWithObjects]
This method steps through the objects and keys arrays, creating entries in the new dictionary as it goes. AnNSInvalidArgumentException is raised if the objects and keys arrays do not have the same number of elements.
Example of [NSDictionary initWithObjects]
NSArray *Ar=[[NSArray alloc]initWithObjects:@"A",@"A",@"A",@"A",@"A",nil];
NSArray *Ar2=[[NSArray alloc]initWithObjects:@1,@2,@3,@4,@5,nil];
NSMutableDictionary *dic2=[[NSMutableDictionary alloc] initWithObjects:Ar forKeys:Ar2];
Example of [NSDictionary initWithObjects]
NSMutableArray *users= [[NSMutableArray alloc] init];

NSArray *keys = [[NSArray alloc] initWithObjects:@"Name", @"Surname", nil];

NSArray *details = [[NSArray alloc] initWithObjects:@"Bob",@"Hope", nil];

NSDictionary *person = [[NSMutableDictionary alloc] initWithObjects:details forKeys:keys];

[users addObject:person];