【问题标题】:Does iPhone SDK Objective C support functions inside of functions?iPhone SDK Objective C 是否支持函数内部的函数?
【发布时间】:2010-03-17 13:42:43
【问题描述】:

我知道 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
    }
  }


}

objective-c 支持这个吗?理论例子:

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

奖励:“本地”功能的正确术语是什么?

【问题讨论】:

  • Javascript 只允许这样做,因为它没有真正的类。这是封装函数的唯一方法。

标签: objective-c iphone methods messages


【解决方案1】:

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

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

虽然有点乱,但确实有效!

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • @NpC0mpl3t3:我在 OP 的问题中没有看到“嵌套”或“方法”这两个词?
    【解决方案3】:

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

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

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

    【讨论】:

    • 请注意,这仅适用于 C,我相信 - 不适用于 Objective-C 或 C++。
    • @Paul 你可以在 Objective-C 方法中嵌套一个 C 函数就好了。您不能将一个方法嵌套在另一个方法中,但这是有道理的。 Objective-C 在普通 C(或 C++)之上是一个非常精简的运行时。
    【解决方案4】:

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

    以下代码 sn-p 演示了如何绘制一组 11x5 的 UILabel。

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

    这是这段代码的输出:

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-20
        • 2021-04-16
        • 2012-05-18
        • 1970-01-01
        相关资源
        最近更新 更多