Creating an NSDictionary - iphone

In the following code, the first log statement shows a decimal as expected, but the second logs NULL. What am I doing wrong?
NSDictionary *entry = [[NSDictionary alloc] initWithObjectsAndKeys:
#"x", [NSNumber numberWithDouble:acceleration.x],
#"y", [NSNumber numberWithDouble:acceleration.y],
#"z", [NSNumber numberWithDouble:acceleration.z],
#"date", [NSDate date],
nil];
NSLog([NSString stringWithFormat:#"%#", [NSNumber numberWithDouble:acceleration.x]]);
NSLog([NSString stringWithFormat:#"%#", [entry objectForKey:#"x"]]);

You are exchanging the order in which you insert objects and key: you need to insert first the object, then the key as shown in the following example.
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:#"value1", #"key1", #"value2", #"key2", nil];

new Objective-c supports this new syntax 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 = Tokyo; UnitedKingdom = London; }

Related

EXC_BAD_ACCESS on NSMutableDictionary

I am beginning with iOS development, I have this code :
First of all I declare the listOfItems NSMutableArray:
#interface SAMasterViewController () {
NSMutableArray *listOfItems;
}
#end
And now, here is the part the code that gives me an "EXC_BAD_ACCESS (code=1, address=0x5fc260000)" error.
The error is given in the last line of the "individual_data" object.
listOfItems = [[NSMutableArray alloc] init];
for(NSDictionary *tweetDict in statuses) {
NSString *text = [tweetDict objectForKey:#"text"];
NSString *screenName = [[tweetDict objectForKey:#"user"] objectForKey:#"screen_name"];
NSString *img_url = [[tweetDict objectForKey:#"user"] objectForKey:#"profile_image_url"];
NSInteger unique_id = [[tweetDict objectForKey:#"id"] intValue];
NSInteger user_id = [[[tweetDict objectForKey:#"user"] objectForKey:#"id"] intValue ];
NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
text, #"text_tweet",
screenName,#"user_name",
img_url, #"img_url",
unique_id, #"unique_id",
user_id, #"user_id", nil];
[listOfItems addObject:individual_data];
}
Thanks in advance.
You can not put NSIntegers or any other non Objective-C class inside of an array or dictionary. You need to wrap them in an NSNumber.
NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
text, #"text_tweet",
screenName,#"user_name",
img_url, #"img_url",
[NSNumber numberWithInteger:unique_id], #"unique_id",
[NSNumber numberWithInteger:user_id], #"user_id", nil];
//Or if you want to use literals
NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
text, #"text_tweet",
screenName,#"user_name",
img_url, #"img_url",
#(unique_id), #"unique_id",
#(user_id), #"user_id", nil];

Getting strange error in NSDictionary

I am getting strange exception when calling the NSDictionary in the following example:
NSInteger userId = 1;
NSDictionary *postData = [NSDictionary dictionaryWithObjectsAndKeys:
#"3", #"a",
#"0", #"b",
userId, #"c",
nil];
Can someone see what's the problem above?
NSDictionary, as most collections, just accepts real Obj-C objects (id type), but you are passing a NSInteger which is a normal C typedef.
Try with NSNumber userId = [NSNumber numberWithInt:1];

object for key based on int number

int number = 3;
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
#"Apple",[NSNumber numberWithInt:0],
#"Peach",[NSNumber numberWithInt:1],
#"Lemon",[NSNumber numberWithInt:2],
#"Pineapple",[NSNumber numberWithInt:3],
........................,
nil];
NSString *numberToKey = [dict objectForKey:number];
How insert in the string the value of the dict based on int number?
The same way you put the values into the dictionary:
NSString *numberToKey = [dict objectForKey:[NSNumber numberWithInt:number]];

How to create an NSDictionary that contains an array for every key?

I wanted to know how (if it is possible) to create an NSDictionary that holds an NSArray for every key.
Something like that:
#"FirstKey" - [0], [1], [2], [3]
#"SecondKey" - [0], [1], [2], [3]
#"ThirdKey" - [0], [1], [2], [3]
#"FourthKey" - [0], [1], [2], [3]
Hope you got the idea, thanks ahead!
You can add NSArray instances (or any other collection) as values of an NSDictionary:
NSDictionary *d = [[NSMutableDictionary alloc] init];
[d setValue:[NSArray arrayWithObjects:
[NSNumber numberWithInteger:0],
[NSNumber numberWithInteger:1],
...,
nil]
forKey:#"FirstKey"];
[d setValue:[NSArray arrayWithObjects:
[NSNumber numberWithInteger:0],
[NSNumber numberWithInteger:1],
...,
nil]
forKey:#"SecondKey"];
//etc.
Alternatively, if you don't have too many keys/values:
NSArray *values1 = [NSArray arrayWithObjects:
[NSNumber numberWithInteger:0],
[NSNumber numberWithInteger:1],
...,
nil];
NSArray *values2 = [NSArray arrayWithObjects:
[NSNumber numberWithInteger:0],
[NSNumber numberWithInteger:1],
...,
nil];
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
values1, #"FirstKey",
values2, #"SecondKey",
...,
nil];
That's totally possibly. Simplest way is to create your NSArray and then just add that as the object for the key to your NSDictionary
NSArray *myArray = [NSArray arrayWithObjects:#"One",#"Two", nil];
[myDictionary setObject:myArray forKey:#"FirstKey"];
Yes, you can store any object in a dictionary. For numeric values you will need to store them as NSValue or NSNumber objects.

Init array with bool values

my attempt to init an array with a number of bool values using:
[myArray initWithObjects:[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
nil];
seems to fail since the debugger shows an empty array after this statement is carried out ... Any clues?
Make sure you are alloc-ing the object, as well, i.e.:
NSArray *myArray = [[NSArray alloc] initWithObjects:...];
...
[myArray release];
Or:
NSArray *myArray = [[[NSArray alloc] initWithObjects:...] autorelease];
Or:
NSArray *myArray = [NSArray arrayWithObjects:...];