将对象的NSArray连接到字符串中,但是需要能够指定属性

时间:2022-02-13 01:48:44

I have a NSArray of Foo objects.

我有一个Foo对象的NSArray。

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

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

我希望能够将所有这些[Foo name]结果合并到一个NSString中。

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

在c#中,我将通过使用LINQ获得这些元素的数组,创建它们的数组,并将其输入String.Join():

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

Is something like this possible in Objective-C?

这在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?

我知道[NSArray componentsJoinedByString],但我如何能够轻松地选择所有对象的[Foo name]属性,而不通过其内容进行手动循环?

1 个解决方案

#1


36  

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

(docs)

(文档)

#1


36  

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

(docs)

(文档)