Does the iPhone SDK Objective-C function inside functions?

I know that javascript, for example, supports functions inside functions, for example:

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 correct term for a "local" function?

+6
methods objective-c iphone messages
source share
5 answers

The usual term is a nested function. gcc supports nested functions as a C extension (disabled by default). I do not think this parameter is available using Objective-C (or C ++) with gcc, although it may not be a good idea to use it (portability, etc.).

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

+8
source share

A little late, but you can put the inline block in a function, which actions act as your nested functions.

 -(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 little dirty, but it works!

+9
source share

By default, Xcode prohibits nested functions.

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

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

+7
source share

The extension of the answer provided by Gui13 is slightly, with the parameters of the object.

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

 // 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 result for this code:

code output

+1
source share

@Moshe You cannot actually provide nested functions inside Objective C. Instead, you can use the function in the latest version of Swift 3, which allows this function. It will look like this:

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

All Articles