Does iPhone SDK Objective C support functions inside of functions?

耗尽温柔 提交于 2019-12-03 15:09:20

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

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!

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";"

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:

@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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!