0

I have an array of custom objects, say Messages.

These messages have an identifierproperty and I'd like to know what's the most optimal way to get an array of NSString from all those identifiers.

Right now, I'm just looping through all the objects and putting their id in another array,

- (NSArray*)getIdentifiersOf:(NSMutableArray*)array
NSMutableArray *arrIdentifiers = [[NSMutableArray alloc]init];

for (Message *msg in array){
     [arrIdentifiers addObject:msg.identifier];
}

return (NSArray*)arrIdentifiers;

Obviously this works, but it seems so redundant I thought you might think of something else.

I usually just need to send an array of Id's as parameter for example, and I was thinking something along the lines of

myArray[@"identifier"]

that could automatically use the array but only that specific property for every object inside it.

0

1 Answer 1

2

Use

- (id)valueForKey:(NSString *)key:

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

NSArray * arrIdentifiers = [array valueForKey:@"identifier"];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.