0

Is there an direct way to convert an NSArray containing NSDictionary into an NSArray containing values of the dictionaries?

I have an array like this:

a = [ { name = "A"},{ name = "B"}, {name = "C" } ]

And I want this:

a = [ "A", "B", "C" ]

I can only think about iterating the previous array, and adding each field into the new mutable one, to ensure the same order. I'm wondering if there is a higher level operation or syntactic sugar that does that.

Thanks.

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
SauloT
  • 153
  • 7
  • I don't think there is build-in `map` function. But there are a number of third-part frameworks provide it. – Bryan Chen Jul 09 '14 at 23:03

1 Answers1

7

The easiest way would be to use KVC:

a = [a valueForKey:@"name"];

From the documentation:

Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Dima
  • 23,484
  • 6
  • 56
  • 83