Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining a NSArray of objects into a string, but need to be able to specify property

I have a NSArray of Foo objects.

 @interface Foo : NSObject
 {
 }
 - (NSString *) name;
 @end

I want to be able to join all these [Foo name] results into one NSString.

In C# I would get a array of these by using LINQ, creating a Array of it, and feeding it to String.Join():

 List<Foo> foo = [..];
 String.Join(",", foo.select(F => F.name()).ToArray());

Is something like this possible in Objective-C?

I know about [NSArray componentsJoinedByString], but how would I just easily select the [Foo name] properties of all the objects without manually looping trough its contents?

like image 514
Peterdk Avatar asked Jul 28 '10 19:07

Peterdk


1 Answers

[[myArray valueForKey:@"name"] componentsJoinedByString:@","]

(docs)

like image 179
cobbal Avatar answered Oct 08 '22 10:10

cobbal