如果我有NSArray,我可以把它放到NSDictionary中吗? 如果是这样,我该怎么做?
答案 0 :(得分:5)
NSDictionary
可以将任何对象用作值,并将符合NSCopying
的任何对象用作键。所以在你的情况下:
NSArray * myArray = [NSArray arrayWithObjects:@"a", @"b", @"c"];
NSDictionary * dict = [NSDictionary dictionaryWithObject:myArray forKey:@"threeLetters"];
NSMutableDictionary * mutableDict = [NSMutableDictionary dictionaryWithCapacity:10];
[mutableDict setObject:myArray forKey:@"threeLetters"];
答案 1 :(得分:1)
如果您从myArray
开始:
NSArray *myArray = [NSArray arrayWithObjects:...];
如果你想要一个可变字典:
NSMutableDictionary *myMutableDictionary = [NSMutableDictionary dictionary];
[myMutableDictionary setObject:myArray forKey:@"myArray"];
如果你只想要一本字典:
NSDictionary *myDictionary = [NSDictionary dictionaryWithObject:myArray forKey:@"myArray"];
答案 2 :(得分:1)