Thursday, May 9, 2013

NSDictionary initWithObjects forKeys count example ios


initWithObjects forKeys count:

Initializes a newly allocated dictionary with count entries.
- (id)initWithObjects:(const id[])objects forKeys:(const id < NSCopying >[])keys count:(NSUInteger)count
Parameters of [NSDictionary initWithObjects forKeys count]
objects
A C array of values for the new dictionary.
keys
A C array of 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.
count
The number of elements to use from the keys and objects arrays. count must not exceed the number of elements in objects or keys.
Discussion of [NSDictionary initWithObjects forKeys count]
This method steps through the objects and keys arrays, creating entries in the new dictionary as it goes. AnNSInvalidArgumentException is raised if a key or value object is nil.
Example of [NSDictionary initWithObjects forKeys count]
id objects[] = {@"hello", @"there", @"person"};
id keys[] = {@"aa", @"bb", @"cc"};

NSDictionary *dict1 = [[NSDictionary alloc] initWithObjects:objects forKeys:keys count:3];

NSLog(@"%@", [dict1 objectForKey: @"bb"]);