iPhone SDK Objective C 是否支持函数内的函数?

发布于 2024-08-25 10:53:56 字数 602 浏览 6 评论 0原文

例如,我知道 javascript 支持函数内部的函数,如下所示:

function doSomething(){

  function doAnothingThing(){
    //this function is redefined every time doSomething() is called and only exists inside doSomething()    
  }

  //you can also stick it inside of conditions

  if(yes){
    function doSomethingElse(){
      //this function only exists if yes is true
    }
  }


}

Does Objective-C support this?理论示例:

 -(void) doSomething:(id) sender{
   -(void) respondToEvent: (id) sender{
     //theoretically? ... please?
   }
}

奖励:“局部”函数的正确术语是什么?

I know that javascript, for example supports functions inside of functions, like so:

function doSomething(){

  function doAnothingThing(){
    //this function is redefined every time doSomething() is called and only exists inside doSomething()    
  }

  //you can also stick it inside of conditions

  if(yes){
    function doSomethingElse(){
      //this function only exists if yes is true
    }
  }


}

Does objective-c support this? Theoretical example:

 -(void) doSomething:(id) sender{
   -(void) respondToEvent: (id) sender{
     //theoretically? ... please?
   }
}

BONUS: What is the proper term for a "local" function?

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

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

发布评论

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

评论(5

梓梦 2024-09-01 10:53:56

有点晚了,但是您可以将内联块放入函数中,这类似于嵌套函数问题。

-(int)addNumbers:(int)a withB:(int)b withC:(int)c {

    // inline block
    int(^inlineaddNumbers)(int, int) = ^(int a, int b) {
        return a + b;
    };

    if( a == 0 ) return inlineaddNumbers(b,c);
    else return inlineaddNumbers(a,c);   
}

虽然有点乱,但是很有效!

A bit late, but you can place an inline block into a function, which kind of acts like your nested function questions.

-(int)addNumbers:(int)a withB:(int)b withC:(int)c {

    // inline block
    int(^inlineaddNumbers)(int, int) = ^(int a, int b) {
        return a + b;
    };

    if( a == 0 ) return inlineaddNumbers(b,c);
    else return inlineaddNumbers(a,c);   
}

It's a bit messy, but it works!

甜心小果奶 2024-09-01 10:53:56

通常的术语是嵌套函数。 gcc 支持嵌套函数作为 C 的扩展(默认情况下禁用)。我认为这个选项不适用于带有 gcc 的 Objective-C(或 C++),即使是,使用它也可能不是一个好主意(可移植性等)。

gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

The usual term is nested function. gcc supports nested functions as an extension to C (disabled by default). I don't think this option is available with Objective-C (or C++) with gcc though, and even if it were it's probably not a good idea to use it (portability etc).

gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

扛起拖把扫天下 2024-09-01 10:53:56

默认情况下,Xcode 不允许嵌套函数。

如果您想打开它们,请打开项目的信息,转到“构建”选项卡,然后将“其他 C 标志”(在标题为“GCC 4.2 - 语言”的部分下)设置为“-fnested-functions”。

(这存储在您的project.pbxproj文件中,形式为“OTHER_CFLAGS =“-fnested-functions”;”

By default Xcode disallows nested functions.

If you want to switch them on, open up the Info for your project, go to the Build tab, and set "Other C flags" (under the section titled "GCC 4.2 - Language") to "-fnested-functions".

(This is stored in your project.pbxproj file as "OTHER_CFLAGS = "-fnested-functions";"

客…行舟 2024-09-01 10:53:56

使用对象参数稍微扩展 Gui13 提供的答案。

以下代码片段演示了如何绘制一组 11x5 UILabels。

// inline block - to be called as a private function 
UILabel *(^createLabel)(CGRect, NSString *, UIColor *) = ^UILabel *(CGRect rect, NSString *txt, UIColor *color) {
    UILabel *lbl = [UILabel new];
    lbl.frame = rect;
    lbl.textColor = color;
    lbl.backgroundColor = [UIColor whiteColor];
    lbl.font = [UIFont systemFontOfSize:30.f];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.text = txt;
    return lbl;
};
// loop to create 11 rows of 5 columns over the whole screen
float w = CGRectGetWidth([UIScreen mainScreen].bounds);
float h = CGRectGetHeight([UIScreen mainScreen].bounds);
float top = h / 10;         //start at 10% from top
float vOffset = h / 13;     //space between rows: 7.6% of screen height
NSArray *xFrom, *xTo;       //columns to start at 5%, 20%, 40%, 60%, 80%
xFrom = @[@(1.f/20),       @(1.f/5),        @(2.f/5),        @(3.f/5),        @(4.f/5)];
xTo   = @[@(1.f/5-1.f/16), @(2.f/5-1.f/16), @(3.f/5-1.f/16), @(4.f/5-1.f/16), @(19.f/20)];
#define SFMT(format...) [NSString stringWithFormat:format]
for (int row=0; row<11; row++) {
    for (int col=0; col<5; col++) {
        CGRect rect = CGRectMake([xFrom[col] floatValue]*w, top+row*vOffset, [xTo[col] floatValue]*w-[xFrom[col] floatValue]*w, vOffset*0.9);
        UILabel *lbl = createLabel(rect, SFMT(@"%i-%i", row, col), [UIColor blueColor]);
        [<my-view> addSubview:lbl];
    }
}

以下是此代码的输出:

代码输出

Expanding the answer provided by Gui13 a little bit, with object parameters.

The following code snippet demonstrates how to draw an 11x5 set of UILabels.

// inline block - to be called as a private function 
UILabel *(^createLabel)(CGRect, NSString *, UIColor *) = ^UILabel *(CGRect rect, NSString *txt, UIColor *color) {
    UILabel *lbl = [UILabel new];
    lbl.frame = rect;
    lbl.textColor = color;
    lbl.backgroundColor = [UIColor whiteColor];
    lbl.font = [UIFont systemFontOfSize:30.f];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.text = txt;
    return lbl;
};
// loop to create 11 rows of 5 columns over the whole screen
float w = CGRectGetWidth([UIScreen mainScreen].bounds);
float h = CGRectGetHeight([UIScreen mainScreen].bounds);
float top = h / 10;         //start at 10% from top
float vOffset = h / 13;     //space between rows: 7.6% of screen height
NSArray *xFrom, *xTo;       //columns to start at 5%, 20%, 40%, 60%, 80%
xFrom = @[@(1.f/20),       @(1.f/5),        @(2.f/5),        @(3.f/5),        @(4.f/5)];
xTo   = @[@(1.f/5-1.f/16), @(2.f/5-1.f/16), @(3.f/5-1.f/16), @(4.f/5-1.f/16), @(19.f/20)];
#define SFMT(format...) [NSString stringWithFormat:format]
for (int row=0; row<11; row++) {
    for (int col=0; col<5; col++) {
        CGRect rect = CGRectMake([xFrom[col] floatValue]*w, top+row*vOffset, [xTo[col] floatValue]*w-[xFrom[col] floatValue]*w, vOffset*0.9);
        UILabel *lbl = createLabel(rect, SFMT(@"%i-%i", row, col), [UIColor blueColor]);
        [<my-view> addSubview:lbl];
    }
}

Here is the output for this code:

code output

可是我不能没有你 2024-09-01 10:53:56

@Moshe你实际上不能在Objective C中提供嵌套函数。相反,你可以使用最新的Swift 3中启用此功能的功能。它将如下所示:

func someFunction(input:String)->String
{
    var inputString = input;
    func complexFunctionOnString()
    {
        inputString = "Hello" + input;
    }
    complexFunctionOnString();
    return inputString;
}
someFunction("User");

@Moshe You cannot actually provide the nested functions inside the Objective C. Instead you can use the feature in latest Swift 3 which enables this feature. It will be like below:

func someFunction(input:String)->String
{
    var inputString = input;
    func complexFunctionOnString()
    {
        inputString = "Hello" + input;
    }
    complexFunctionOnString();
    return inputString;
}
someFunction("User");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文