开发者

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 NSCopyingas 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜