Objective-C 中 JS 的 `map()` 函数的等价物是什么?

发布于 2024-10-12 21:11:15 字数 90 浏览 7 评论 0原文

JS 的 map() 函数在 Objective-C 中的等价物是什么?我会只使用 NSFastEnumeration 并自己应用该函数吗?

What's the Objective-C equivalent of JS's map() function? Would I just use NSFastEnumeration and apply the function myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请恋爱 2024-10-19 21:11:15

您可以使用 NSArray 的 enumerateObjectsUsingBlock: 如果您使用的是 OS X 10.6 或 iOS 4.:

NSMutableArray *mapped = [NSMutableArray arrayWithCapacity:[array count]];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    id mapObj = DoSomethingToObject(obj);
    [mapped addObject:mapObj];
}];

(无耻但相关插件:我有一个适用于 OS X 和 iOS 的 library ,它向 NSArray 添加了地图和其他类似功能NSSet。)

You can use NSArray's enumerateObjectsUsingBlock: if you're on OS X 10.6 or iOS 4.:

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 library for OS X and iOS that adds map and other similar functionality to NSArray and NSSet.)

ぽ尐不点ル 2024-10-19 21:11:15

这取决于。比如说,如果您有一个对象数组,并且这些对象具有 URL 属性(例如),那么您可以这样做:

NSArray * urls = [myArray valueForKey:@"URL"];

同样,如果您可以通过对于采用 0 或 1 个参数的单个消息,您可以执行以下操作:

[myArray makeObjectsPerformSelector:@selector(doFoo)];
//or:
[myArray makeObjectsPerformSelector:@selector(doFooWithBar:) withObject:aBar];

对于除此之外的任何内容,您必须自己迭代对象。您可以使用 for() 循环、for(in) 循环或类似 -enumerateObjectsUsingBlock: 等。

It depends. If you have, say, an array of objects, and these objects have a URL property (for example), then you can do:

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:

[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.

错々过的事 2024-10-19 21:11:15

你自己做吧。没有一种方法可以满足您的需求。

编辑:对于那些投反对票的人,这是当时(三年前)的正确答案,并且仍然适用于 Objective-C,但 Swift 确实有一个 map() 函数。

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.

黯淡〆 2024-10-19 21:11:15

查看BlocksKit,它为NSArray提供了map、reduce和filer。

  • (NSArray *)map:(BKTransformBlock)block;
  • (id)reduce:(id)initial withBlock:(BKAAccumulationBlock)block;
  • (NSArray *)select:(BKValidationBlock)block;

Check BlocksKit, it provides map, reduce and filer for NSArray.

  • (NSArray *)map:(BKTransformBlock)block;
  • (id)reduce:(id)initial withBlock:(BKAccumulationBlock)block;
  • (NSArray *)select:(BKValidationBlock)block;
凶凌 2024-10-19 21:11:15

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;
}

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;
}

Category function for NSArray an alternative

- (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 NSMutableArray an alternative

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文