如何在 Objective-C 中编写 lambda 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/943992/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:39:29  来源:igfitidea点击:

How to write lambda methods in Objective-C?

objective-cfunctional-programminglambda

提问by AlBlue

How to write lambda methods in Objective-C ?

如何在 Objective-C 中编写 lambda 方法?

回答by AlBlue

The concept of a lambda in Objective-C is now encapsulated with the idea of Blockswhich are the equivalent of pass-by-reference functions. Of course, arguably one had that already in C with the idea of function pointers; blocks are just a way of also capturing local state (i.e. can be closures). In fact, blocks can also be used in other C languages as well (on Mac) - there's a proposal to make them part of the standard C syntax.

Objective-C 中 lambda 的概念现在封装在Blocks的概念中,Blocks相当于传递引用函数。当然,可以说在 C 语言中已经有了函数指针的概念;块只是一种捕获本地状态的方式(即可以是闭包)。事实上,块也可以用在其他 C 语言中(在 Mac 上)——有一个建议让它们成为标准 C 语法的一部分。

Here's an example of defining a lambda to multiply two numbers together:

这是一个定义 lambda 以将两个数字相乘的示例:

int (^mult)(int, int) = ^(int a, int b) { return a*b; };

The first part declares a variable, of type ^int(int,int)and then assigns it to the lambda expression (aka block) which returns the multiple of its two arguments. You can then pass that fn around, define it in other places etc; you can even use it in other functions.

第一部分声明一个类型的变量,^int(int,int)然后将其分配给 lambda 表达式(又名块),该表达式返回其两个参数的倍数。然后你可以传递那个 fn,在其他地方定义它等等;您甚至可以在其他功能中使用它。

Here's an example of defining a function, which when invoked, returns another function:

这是一个定义函数的示例,该函数在调用时返回另一个函数:

multiplyBy = ^(int a) { return ^(int b) { return b*a; }; };
triple = multiplyBy(3);

Note that you can intermix blocks with object types (usually using idas the object type) and many of the new Objective-C object data structures have some kind of block-level operation. GCD also uses blocks in order to pass in arbitrary events; however, note that GCD can also be used with function pointers as well.

请注意,您可以将块与对象类型(通常id用作对象类型)混合,并且许多新的 Objective-C 对象数据结构具有某种块级操作。GCD 还使用块来传递任意事件;但是,请注意 GCD 也可以与函数指针一起使用。

回答by Will Harris

OS X 10.6 introduced blocks. See AlBlue's answer for examples.

OS X 10.6 引入了块。有关示例,请参阅AlBlue 的回答

If you're not using Snow Leopard, you can get something close to function composition using various other features.

如果您不使用 Snow Leopard,您可以使用各种其他功能获得接近于函数组合的东西。

Example using C function pointers:

使用 C 函数指针的示例:

void sayHello() {
    NSLog(@"Hello!");
}

void doSomethingTwice(void (*something)(void)) {
    something();
    something();
}

int main(void) {
    doSomethingTwice(sayHello);
    return 0;
}

Example using the command pattern:

使用命令模式的示例:

@protocol Command <NSObject>
- (void) doSomething;
@end

@interface SayHello : NSObject <Command> {
}
@end

@implementation SayHello
- (void) doSomething {
    NSLog(@"Hello!");    
}
@end

void doSomethingTwice(id<Command> command) {
    [command doSomething];
    [command doSomething];
}

int main(void) {
    SayHello* sayHello = [[SayHello alloc] init];
    doSomethingTwice(sayHello);
    [sayHello release];
    return 0;
}

Example using a selector:

使用选择器的示例:

@interface SaySomething : NSObject {
}
- (void) sayHello;
@end

@implementation SaySomething
- (void) sayHello {
    NSLog(@"Hello!");    
}
@end

void doSomethingTwice(id<NSObject> obj, SEL selector) {
    [obj performSelector:selector];
    [obj performSelector:selector];
}

int main(void) {
    SaySomething* saySomething = [[SaySomething alloc] init];
    doSomethingTwice(saySomething, @selector(sayHello));
    [saySomething release];
    return 0;
}

回答by Abizern

I heard André Pang at NSConference talking about how blocks were going to be introduced with the next version of Objective-C.

我在 NSConference 上听到 André Pang 谈到下一个版本的 Objective-C 将如何引入块。

This should allow functional programming.

这应该允许函数式编程。

Edit: Since Snow Leopard has been released, this is indeed the case. Objective-C now has Blocks.

编辑:自从雪豹发布以来,情况确实如此。Objective-C 现在有Blocks