如何创建一个包含每个键的数组的 NSDictionary?

发布于 2024-11-17 14:33:04 字数 242 浏览 1 评论 0原文


我想知道如何(如果可能的话)创建一个为每个键保存一个 NSArray 的 NSDictionary 。

类似的东西:

@“FirstKey” - [0],[1],[2],[3]
@"SecondKey" - [0], [1], [2], [3]
@“第三键” - [0]、[1]、[2]、[3]
@“FourthKey” - [0]、[1]、[2]、[3]

希望您明白了,谢谢!

I wanted to know how (if it is possible) to create an NSDictionary that holds an NSArray for every key.

Something like that:

@"FirstKey" - [0], [1], [2], [3]
@"SecondKey" - [0], [1], [2], [3]
@"ThirdKey" - [0], [1], [2], [3]
@"FourthKey" - [0], [1], [2], [3]

Hope you got the idea, thanks ahead!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假扮的天使 2024-11-24 14:33:04

您可以添加 NSArray 实例(或任何其他集合)作为 NSDictionary 的值:

NSDictionary *d = [[NSMutableDictionary alloc] init];
[d setValue:[NSArray arrayWithObjects:
               [NSNumber numberWithInteger:0], 
               [NSNumber numberWithInteger:1],
               ...,
               nil]
     forKey:@"FirstKey"];
[d setValue:[NSArray arrayWithObjects:
               [NSNumber numberWithInteger:0], 
               [NSNumber numberWithInteger:1],
               ...,
               nil]
     forKey:@"SecondKey"];
//etc.

或者,如果您没有太多键/值:

NSArray *values1 = [NSArray arrayWithObjects:
                    [NSNumber numberWithInteger:0], 
                    [NSNumber numberWithInteger:1],
                    ...,
                    nil];
NSArray *values2 = [NSArray arrayWithObjects:
                    [NSNumber numberWithInteger:0], 
                    [NSNumber numberWithInteger:1],
                    ...,
                    nil];
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
                     values1, @"FirstKey",
                     values2, @"SecondKey",
                     ...,
                     nil];

You can add NSArray instances (or any other collection) as values of an NSDictionary:

NSDictionary *d = [[NSMutableDictionary alloc] init];
[d setValue:[NSArray arrayWithObjects:
               [NSNumber numberWithInteger:0], 
               [NSNumber numberWithInteger:1],
               ...,
               nil]
     forKey:@"FirstKey"];
[d setValue:[NSArray arrayWithObjects:
               [NSNumber numberWithInteger:0], 
               [NSNumber numberWithInteger:1],
               ...,
               nil]
     forKey:@"SecondKey"];
//etc.

Alternatively, if you don't have too many keys/values:

NSArray *values1 = [NSArray arrayWithObjects:
                    [NSNumber numberWithInteger:0], 
                    [NSNumber numberWithInteger:1],
                    ...,
                    nil];
NSArray *values2 = [NSArray arrayWithObjects:
                    [NSNumber numberWithInteger:0], 
                    [NSNumber numberWithInteger:1],
                    ...,
                    nil];
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
                     values1, @"FirstKey",
                     values2, @"SecondKey",
                     ...,
                     nil];
倾`听者〃 2024-11-24 14:33:04

这完全有可能。最简单的方法是创建 NSArray,然后将其添加为 NSDictionary 的键的对象

NSArray *myArray = [NSArray arrayWithObjects:@"One",@"Two", nil];
[myDictionary setObject:myArray forKey:@"FirstKey"];

That's totally possibly. Simplest way is to create your NSArray and then just add that as the object for the key to your NSDictionary

NSArray *myArray = [NSArray arrayWithObjects:@"One",@"Two", nil];
[myDictionary setObject:myArray forKey:@"FirstKey"];
冬天的雪花 2024-11-24 14:33:04

是的,您可以在字典中存储任何对象。对于数值,您需要将它们存储为 NSValue 或 NSNumber 对象。

Yes, you can store any object in a dictionary. For numeric values you will need to store them as NSValue or NSNumber objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文