21

Is there any of way (other than looping) of extracting a particular property of all objects in an array. So say there in an array of people. I want to extract all their first names into an array.

2 Answers 2

40

Key Value coding will help you with that:

NSArray *result = [people valueForKey:@"firstname"];
Sign up to request clarification or add additional context in comments.

2 Comments

Big +1 to JustSid. Works great. Note: people must be an NSArray to be true. If people is a NSSet the result will be a NSSet
How to achieve the same in swift ?
-1

I got answer for my question. This is how we can achieve the same in swift.

let arraytWithProperties = arrayWithObjects.map{ $0.propertyName }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.