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

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.

Related

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]];

Insert NSArray into an NSDictionary

If I have a NSArray, can I put this into a NSDictionary?
If so, how can I do this?
An NSDictionary can use any objects as values, and any objects that conforms to NSCopyingas keys. So in your case:
NSArray * myArray = [NSArray arrayWithObjects:#"a", #"b", #"c"];
NSDictionary * dict = [NSDictionary dictionaryWithObject:myArray forKey:#"threeLetters"];
NSMutableDictionary * mutableDict = [NSMutableDictionary dictionaryWithCapacity:10];
[mutableDict setObject:myArray forKey:#"threeLetters"];
If you start with myArray:
NSArray *myArray = [NSArray arrayWithObjects:...];
If you want a mutable dictionary:
NSMutableDictionary *myMutableDictionary = [NSMutableDictionary dictionary];
[myMutableDictionary setObject:myArray forKey:#"myArray"];
If you just want a dictionary:
NSDictionary *myDictionary = [NSDictionary dictionaryWithObject:myArray forKey:#"myArray"];
Maybe you should read this:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Collections/Collections.html#//apple_ref/doc/uid/10000034i

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:...];

Creating an NSDictionary

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; }