views:

321

answers:

1

This actually could be a multipart question. But here's the first part ...

I have an array (actually in a plist) of dictionaries. Each dictionary has 3 keys in it: (title), (points), and (description).

I am trying to make a NEW array with the values of the key "title" from each dictionary in that first array.

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.

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.

Thanks in advance (and yes I am a newbie)

+1  A: 
[array valueForKeyPath:@"title"]

That should return a new array with the result of calling [obj valueForKey:@"title"] on each object in the array.

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.

BJ Homer
And that will run through every dictionary in my array? like do a ...newArray = [mainArray valueForKey:@"title"]I come from 11 years (many moons ago) of procedural background (VB, DOS, etc.). So this is taking me some getting used to.My first instinct was to do some sort of for statement to cycle through all the dictionaries in my "mainArray" and do an then just add the titles one by one to a new array.Pardon my ignorance. Just confused about your posted method cycling through the entire array.Man I wish iPhone had safari style drop down combo boxes LOL
tbird412
Yes, it will go through every object in your array and get the "title" attribute. See the "valueForKey:" documentation on NSArray. You could, of course, loop through one-by-one and construct a new array with the titles; that wouldn't be difficult. But "valueForKey:" is already doing that for you.
BJ Homer