[ACCEPTED]-Creating an NSDictionary-cocoa-touch
Accepted answer
You are exchanging the order in which you 3 insert objects and key: you need to insert 2 first the object, then the key as shown 1 in the following example.
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
new Objective-c supports this new syntax 1 for static initialisation.
@{key:value}
For example:
NSDictionary* dict = @{@"x":@(acceleration.x), @"y":@(acceleration.y), @"z":@(acceleration.z), @"date":[NSDate date]};
NSDictionary Syntax:
NSDictionary *dictionaryName = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@value2",@"key2", nil];
Example:
NSDictionary *importantCapitals = [NSDictionary dictionaryWithObjectsAndKeys:
@"NewDelhi",@"India",@"Tokyo",@"Japan",@"London",@"UnitedKingdom", nil];
NSLog(@"%@", importantCapitals);
Output looking like,
{India = NewDelhi; Japan 1 = Tokyo; UnitedKingdom = London; }
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.