将NSArray插入NSDictionary

时间:2010-07-31 09:49:29

标签: iphone objective-c nsarray nsdictionary

如果我有NSArray,我可以把它放到NSDictionary中吗? 如果是这样,我该怎么做?

3 个答案:

答案 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)