在Objective C中等效Swift的.map? [重复]

时间:2022-10-25 15:02:47

This question already has an answer here:

这个问题在这里已有答案:

Say I have NSArray *x = @[@1, @2, @3, @4];

说我有NSArray * x = @ [@ 1,@ 2,@ 3,@ 4];

Now say I want an array like @[@2, @4, @6, @8]

现在说我想要一个像@ [@ 2,@ 4,@ 6,@ 8]的数组

In good ole Swift, I can just do :

在好的斯威夫特,我可以这样做:

xDoubled = x.map({($0) * 2})

Can someone tell me how I can do this in Objective-C without doing -

有人能告诉我如何在Objective-C中做到这一点而不做 -

NSMutableArray *xDoubled = [NSMutableArray new];
for (NSInteger xVal in x) {
    [xDoubled addObject:xVal * 2];
}

?

2 个解决方案

#1


3  

NSArray doesn't have a map method. It has an enumerateObjectsUsingBlock: method (and related ones) that do something similar; they don't automatically convert an object into another and return another array, but you can do that manually easily enough. However, they're not all that much different than your example.

NSArray没有map方法。它有一个enumerateObjectsUsingBlock:方法(和相关的)做类似的事情;它们不会自动将对象转换为另一个对象并返回另一个数组,但您可以轻松地手动完成。但是,它们与你的例子并没有太大的不同。

I wrote a library called collections that adds a map method (and other collections-oriented methods) to NSArray, NSDictionary, and NSSet, though.

我编写了一个名为collections的库,它将一个map方法(以及其他面向集合的方法)添加到NSArray,NSDictionary和NSSet中。

#2


1  

I don't necessarily recommend this, but you can do some funky stuff leveraging categories and Key-Value Coding. For example:

我不一定推荐这个,但你可以利用类别和键值编码做一些时髦的东西。例如:

@interface NSNumber (DoubledValue)
- (NSNumber*) doubledValue;
@end

@implementation NSNumber (DoubledValue)
- (NSNumber*) doubledValue
{
    return @([self doubleValue] * 2);
}
@end

xDoubled = [x valueForKey:@"doubledValue"];

In general, for any operation that can be expressed as a key or key path to a property of the object, -valueForKey[Path]: acts as a primitive form of map().

通常,对于任何可以表示为对象属性的键或键路径的操作,-valueForKey [Path]:充当map()的原始形式。

#1


3  

NSArray doesn't have a map method. It has an enumerateObjectsUsingBlock: method (and related ones) that do something similar; they don't automatically convert an object into another and return another array, but you can do that manually easily enough. However, they're not all that much different than your example.

NSArray没有map方法。它有一个enumerateObjectsUsingBlock:方法(和相关的)做类似的事情;它们不会自动将对象转换为另一个对象并返回另一个数组,但您可以轻松地手动完成。但是,它们与你的例子并没有太大的不同。

I wrote a library called collections that adds a map method (and other collections-oriented methods) to NSArray, NSDictionary, and NSSet, though.

我编写了一个名为collections的库,它将一个map方法(以及其他面向集合的方法)添加到NSArray,NSDictionary和NSSet中。

#2


1  

I don't necessarily recommend this, but you can do some funky stuff leveraging categories and Key-Value Coding. For example:

我不一定推荐这个,但你可以利用类别和键值编码做一些时髦的东西。例如:

@interface NSNumber (DoubledValue)
- (NSNumber*) doubledValue;
@end

@implementation NSNumber (DoubledValue)
- (NSNumber*) doubledValue
{
    return @([self doubleValue] * 2);
}
@end

xDoubled = [x valueForKey:@"doubledValue"];

In general, for any operation that can be expressed as a key or key path to a property of the object, -valueForKey[Path]: acts as a primitive form of map().

通常,对于任何可以表示为对象属性的键或键路径的操作,-valueForKey [Path]:充当map()的原始形式。