Mos*_*she 6 iphone methods objective-c messages
我知道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
}
}
}
Run Code Online (Sandbox Code Playgroud)
Objective-c是否支持此功能?理论范例:
-(void) doSomething:(id) sender{
-(void) respondToEvent: (id) sender{
//theoretically? ... please?
}
}
Run Code Online (Sandbox Code Playgroud)
奖励:"本地"功能的正确用语是什么?
通常的术语是嵌套函数.gcc支持嵌套函数作为C的扩展(默认情况下禁用).我认为这个选项不适用于带有gcc的Objective-C(或C++),即使使用它可能也不是一个好主意(可移植性等).
gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
有点晚了,但你可以将一个内联块放入一个函数中,这类似于你的嵌套函数问题.
-(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);
}
Run Code Online (Sandbox Code Playgroud)
它有点乱,但它有效!
默认情况下,Xcode不允许嵌套函数.
如果要打开它们,请打开项目的Info,转到Build选项卡,然后将"Other C flags"(在"GCC 4.2 - Language"部分下面)设置为"-fnested-functions".
(它作为"OTHER_CFLAGS ="-fnested-functions";"存储在project.pbxproj文件中