简体   繁体   English

Xcode Obj-C:从一个字典数组中每个键创建一个新的NSArray

[英]Xcode Obj-C: Make new NSArray from one key each out of an Array of Dictionaries

This actually could be a multipart question. 这实际上可能是一个多方面的问题。 But here's the first part ... 但是这是第一部分...

I have an array (actually in a plist) of dictionaries. 我有一组字典(实际上是在plist中)。 Each dictionary has 3 keys in it: (title), (points), and (description). 每个词典中都有3个键:(标题),(点)和(描述)。

I am trying to make a NEW array with the values of the key "title" from each dictionary in that first array. 我正在尝试使用第一个数组中每个字典的键“ title”的值创建一个NEW数组。

Let me explain WHY I am doing this and maybe that will provide a better all around explanation. 让我解释一下为什么这样做,也许这将为您提供更好的全面解释。

I am trying to let people pick from a pre-determined list. 我试图让人们从预定列表中选择。 Heck, if this was a web page it would be very simple since all I really care about are the "points" and the "Title". 哎呀,如果这是一个网页,那将非常简单,因为我真正关心的只是“要点”和“标题”。 On a web site I could simply do a drop down combo-box with the "points" being the value and the title being the text for each row. 在网站上,我可以简单地做一个下拉组合框,其中“点”是值,标题是每一行的文本。

But this is not a web page. 但这不是网页。

So what I am trying to do here is pop out a modal picker when they click the text field. 因此,我要在此处执行的操作是单击文本字段时弹出一个模式选择器。 The modal picker shows the alphabetical ordered "titles" from our new array. 模态选择器显示新数组中按字母顺序排列的“标题”。 And whichever one they select, it closes the modal view and assigns that "title" text to the UITextField which cannot be edited by the user. 无论选择哪一个,它都会关闭模式视图,并将该“标题”文本分配给用户无法编辑的UITextField

I have some code to get my modal picker to pop out. 我有一些代码可以让我的模式选择器弹出。 But I need to feed it an array of just the "titles" of each dictionary in my real array. 但是我需要给它提供一个仅包含我实际数组中每个词典的“标题”的数组。

[array valueForKeyPath:@"title"]

That should return a new array with the result of calling [obj valueForKey:@"title"] on each object in the array. 那应该返回一个新数组,并在数组中的每个对象上调用[obj valueForKey:@“ title”]。

In this case, you should be able to just use 在这种情况下,您应该可以使用

[array valueForKey:@"title"]

as well, but if you wanted to go further into the dictionary (like "person.name" or something), the key-path version is very nice. 同样,但是如果您想进一步学习字典(例如“ person.name”之类的东西),则密钥路径版本非常好。

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

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