Tuesday, May 7, 2013

NSDictionary dictionaryWithObject example ios


dictionaryWithObject :forKey:

Creates and returns a dictionary containing a given key and value.
+ (id)dictionaryWithObject:(id)anObject forKey:(id < NSCopying >)aKey
Parameters of [NSDictionary dictionaryWithObject]
anObject
The value corresponding to aKey.
If this value is nil, an NSInvalidArgumentException is raised.
aKey
The key for anObject.
If this value is nil, an NSInvalidArgumentException is raised.
Return Value
A new dictionary containing a single object, anObject, for a single key, aKey.
Example of [NSDictionary dictionaryWithObject]
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects 
                                                       forKeys:keys];
Example of [NSDictionary dictionaryWithObject]
NSObject * obj = [[NSObject alloc] init];
NSDictionary * dict = [NSDictionary dictionaryWithObject :obj forKey:@"test"];