gpt4 book ai didi

objective-c - 是否可以在 Objective-C 中将一个函数嵌套在另一个函数中

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:35 25 4
gpt4 key购买 nike

Objective-C 是 ANSI C 的超集。在 GNU C 中,you can nest one function in another function ...像这样...

float E(float x)
{
float F(float y) //objective-C compiler says expected ; at end of declaration
{
return x + y;
}
return F(3) + F(4);
}

是否可以在 Objective-C 中执行此操作?

我知道 block 和 NSInvocation 对象,它们可以模拟上面的 C 代码。但是你能在 Objective-C 中的另一个函数的词法范围内定义一个函数吗?有点像……

-(int) outer:(int)x
{
-(int) inner:(int)y //use of undeclared identifier inner
{
return x * 3;
}
return inner(x);
}

最佳答案

你不能,但你可以很容易地用 block 获得相同的行为

int outer(int i)
{
int (^inner)(int) = ^(int x)
{
return x * 3;
};

return inner(i);
}

关于objective-c - 是否可以在 Objective-C 中将一个函数嵌套在另一个函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13547306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com