hi, I have a question. If i have a NSArray i can put into a NSDictionary? If i can, how can do these?
Thanks
hi, I have a question. If i have a NSArray i can put into a NSDictionary? If i can, how can do these?
Thanks
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"];
An NSDictionary
can use any objects as values, and any objects that conforms to NSCopying
as 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"];
Maybe you should read this: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Collections/Collections.html#//apple_ref/doc/uid/10000034i