如何在 Objective C 中获得字典列表?

发布于 2024-11-17 09:30:43 字数 332 浏览 1 评论 0原文

这是 python 中的一个示例,

    temy_dict_list = []
my_dict_list.append({'text':'first value', 'value':'number 1'})
my_dict_list.append({'text':'second value', 'value':'number 2'})
my_dict_list.append({'text':'third value', 'value':'number 3'})

我想在 Objective-C 中实现与此相同的东西!

我怎样才能实现这个?我想要一个字典列表!

here is a an example in python

    temy_dict_list = []
my_dict_list.append({'text':'first value', 'value':'number 1'})
my_dict_list.append({'text':'second value', 'value':'number 2'})
my_dict_list.append({'text':'third value', 'value':'number 3'})

I want to implement something the same as this in objective-c !

how can i implement this ? i want a list of dictionaries!

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

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

发布评论

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

评论(3

蛮可爱 2024-11-24 09:30:43
NSMutableArray* list = [NSMutableArray array];
[list addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"first value",@"text", @"number 1",@"value", nil]];
[list addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"second value",@"text", @"number 2",@"value", nil]];
[list addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"third value",@"text", @"number 3",@"value", nil]];
NSMutableArray* list = [NSMutableArray array];
[list addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"first value",@"text", @"number 1",@"value", nil]];
[list addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"second value",@"text", @"number 2",@"value", nil]];
[list addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"third value",@"text", @"number 3",@"value", nil]];
一城柳絮吹成雪 2024-11-24 09:30:43
NSArray *array = [NSArray arrayWithObjects:dict1,dict2,nil]
NSArray *array = [NSArray arrayWithObjects:dict1,dict2,nil]
浮生面具三千个 2024-11-24 09:30:43

我不知道Python(你的代码对我来说似乎是错误的),但我猜你想要一个字典数组,它被翻译为拥有一个NSArrayNSMutableArray 在这种情况下)它保存一个或多个 NSDictionary 对象。

您可以创建一个 NSMutableArray 实例并向其添加一些对象。

NSMutableArray *anArray = [[NSMutableArray alloc] init];
[anArray addObject:[NSDictionary dictionaryWithObjectAndKeys: @"first-value", @"text", @"number-1", @"value", nil]];
[anArray addObject:[NSDictionary dictionaryWithObjectAndKeys: @"second-value", @"text", @"number-2", @"value", nil]];
[anArray addObject:[NSDictionary dictionaryWithObjectAndKeys: @"third-value", @"text", @"number-3", @"value", nil]];
// ...
// don't forget to release anArray
[anArray release];

我们使用类方法 dictionaryWithObjectAndKeys 创建一个 NSDictionary;我们传递字典将保存的以 nil 结尾的值和键列表。

I don't know python (and your code seems wrong to me), but I guess you want to have an array of dictionaries, which is translated to having an NSArray (NSMutableArray in this case) which holds one ore more NSDictionary objects.

You can create an NSMutableArray instance and add add to it some objects.

NSMutableArray *anArray = [[NSMutableArray alloc] init];
[anArray addObject:[NSDictionary dictionaryWithObjectAndKeys: @"first-value", @"text", @"number-1", @"value", nil]];
[anArray addObject:[NSDictionary dictionaryWithObjectAndKeys: @"second-value", @"text", @"number-2", @"value", nil]];
[anArray addObject:[NSDictionary dictionaryWithObjectAndKeys: @"third-value", @"text", @"number-3", @"value", nil]];
// ...
// don't forget to release anArray
[anArray release];

We create an NSDictionary with the class method dictionaryWithObjectAndKeys; we pass a nil-terminated list of values and keys that the dictionary will hold.

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