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 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"];
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
精彩评论