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

发布于 2024-07-23 06:01:24 字数 37 浏览 2 评论 0原文

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

How to write lambda methods in Objective-C ?

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

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

发布评论

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

评论(4

诗化ㄋ丶相逢 2024-07-30 06:01:24

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

下面是定义 lambda 将两个数字相乘的示例:

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

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

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

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

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

The concept of a lambda in Objective-C is now encapsulated with the idea of Blocks which 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.

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

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.

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 id as 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.

何必那么矫情 2024-07-30 06:01:24

OS X 10.6 引入了块。 请参阅AlBlue 的答案示例

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

使用 C 函数指针的示例:

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

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

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

使用命令模式的示例:

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

使用选择器的示例:

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

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

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

Example using C function pointers:

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;
}
給妳壹絲溫柔 2024-07-30 06:01:24

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

这应该允许函数式编程。

编辑:自从雪豹发布以来,确实是这样。 Objective-C 现在有

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

This should allow functional programming.

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

初心未许 2024-07-30 06:01:24

好吧,我认为你可以这样编写你的匿名函数:

myBlock = ^( int number )
{
    return [ NSString stringWithFormat: @"Passed number: %i", number ];
};

为此,你需要添加以下语法:

NSString * ( ^ myBlock )( int );

一般来说,这个链接为 obj-c 和 obj-c++ 写了一些东西,我希望它会对你有所帮助。

https://xs-labs.com/en/archives/articles/objc -块/

Well, I think you can write your anonymous function like this:

myBlock = ^( int number )
{
    return [ NSString stringWithFormat: @"Passed number: %i", number ];
};

For this, you need to add the following syntax:

NSString * ( ^ myBlock )( int );

In general, this link has written something for obj-c and obj-c++, I hope it will help you.

https://xs-labs.com/en/archives/articles/objc-blocks/

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