javascript JS 的`map()` 函数的Objective-C 等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4741779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the Objective-C equivalent of JS's `map()` function?
提问by Moshe
What's the Objective-C equivalent of JS's map()function? Would I just use NSFastEnumeration and apply the function myself?
JSmap()函数的 Objective-C 等价物是什么?我会只使用 NSFastEnumeration 并自己应用该函数吗?
回答by mipadi
You can use NSArray's enumerateObjectsUsingBlock:if you're on OS X 10.6 or iOS 4.:
如果您使用NSArray的enumerateObjectsUsingBlock:是 OS X 10.6 或 iOS 4,则可以使用's :
NSMutableArray *mapped = [NSMutableArray arrayWithCapacity:[array count]];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id mapObj = DoSomethingToObject(obj);
[mapped addObject:mapObj];
}];
(Shameless but relevant plug: I have a libraryfor OS X and iOS that adds map and other similar functionality to NSArrayand NSSet.)
(无耻的,但相关的插件:我有一个库为OS X和iOS,增加了地图和其他类似的功能NSArray和NSSet。)
回答by Dave DeLong
It depends. If you have, say, an array of objects, and these objects have a URLproperty (for example), then you can do:
这取决于。如果你有一个对象数组,并且这些对象有一个URL属性(例如),那么你可以这样做:
NSArray * urls = [myArray valueForKey:@"URL"];
Likewise, if you can trigger the behavior of the objects in question via a single message that takes 0 or 1 parameters, you can do:
同样,如果您可以通过带有 0 或 1 个参数的单个消息触发相关对象的行为,您可以执行以下操作:
[myArray makeObjectsPerformSelector:@selector(doFoo)];
//or:
[myArray makeObjectsPerformSelector:@selector(doFooWithBar:) withObject:aBar];
For anything beyond that, you'll have to iterate over the objects yourself. You can use a for()loop, a for(in)loop, or something like -enumerateObjectsUsingBlock:, etc.
对于除此之外的任何事情,您必须自己迭代对象。您可以使用for()循环、for(in)循环或类似的东西等-enumerateObjectsUsingBlock:。
回答by Jonathan Grynspan
You do it yourself. There is no single method equivalent to what you want.
你自己做。没有与您想要的等效的单一方法。
Edit: For those downvoting, this was the correct answer at the time (three years ago) and still is for Objective-C, but Swift does have a map()function.
编辑:对于那些投反对票的人,这是当时(三年前)的正确答案,并且仍然适用于 Objective-C,但 Swift 确实有一个map()功能。
回答by Hai Feng Kao
Check BlocksKit, it provides map, reduce and filer for NSArray.
检查BlocksKit,它为 NSArray 提供 map、reduce 和 filer。
- (NSArray *)map:(BKTransformBlock)block;
- (id)reduce:(id)initial withBlock:(BKAccumulationBlock)block;
- (NSArray *)select:(BKValidationBlock)block;
- (NSArray *)map:(BKTransformBlock)block;
- (id)reduce:(id)initial withBlock:(BKAccumulationBlock)block;
- (NSArray *)select:(BKValidationBlock)block;
回答by Nicolas Manzini
Category function for NSArrayan alternative
NSArray的分类函数的另一种选择
- (NSArray *)map:(id(^)(id, BOOL *))block {
NSMutableArray * array = [NSMutableArray array];
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id newObject = block(obj,stop);
if (newObject == nil) {
newObject = [NSNull null];
}
[array addObject:newObject];
}];
return array.copy;
}
Category function for NSMutableArrayan alternative
NSMutableArray的分类函数替代
- (NSMutableArray *)map:(id(^)(id))block {
NSEnumerator * enumerator = ((NSArray *)self.copy).objectEnumerator;
id obj; NSUInteger idx = 0;
while ((obj = enumerator.nextObject)) {
self[idx] = block(obj) ?: [NSNull null];
idx++;
}
return self;
}

