initWithObjects forKeys
Initializes a newly allocated dictionary with entries constructed from the contents of the objects and keys arrays.
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 theNSCopying
protocol), and the copy is added to the new dictionary.
Discussion of [NSDictionary initWithObjects forKeys]
This method steps through the objects and keys arrays, creating entries in the new dictionary as it goes. An
NSInvalidArgumentException
is raised if the objects and keys arrays do not have the same number of elements.
Example of [NSDictionary initWithObjects forKeys]
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 forKeys]
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];