简体   繁体   English

将NSArray插入NSDictionary

[英]Insert NSArray into an NSDictionary

If I have a NSArray, can I put this into a NSDictionary? 如果我有NSArray,我可以把它放到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. NSDictionary可以将任何对象用作值,并将任何符合NSCopying用作键。 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 : 如果你从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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM