views:

48

answers:

3

hi, I have a question. If i have a NSArray i can put into a NSDictionary? If i can, how can do these?

Thanks

+1  A: 

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"];
Alex Reynolds
A: 

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"];
Felixyz
NSDictionary cannot use just any objects as keys. They have to conform to `NSCopying`.
JeremyP
@JeremyP: yes indeed. Corrected.
Felixyz