2010-03-17 116 views
6

我知道的JavaScript,例如支持功能的功能裏面,像這樣:iPhone SDK Objective C支持函數內部的函數嗎?

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? 
    } 
} 

獎金:什麼是「本地」功能的合適名詞?

+0

的Javascript只允許這一點,因爲它沒有真正的類。這是封裝函數的唯一方法。 – TechZen 2010-03-17 16:11:15

回答

8

通常的術語是嵌套函數。 gcc支持嵌套函數作爲C的擴展(默認爲禁用)。我認爲這個選項不適用於使用gcc的Objective-C(或C++),儘管它可能不是一個好主意(可移植性等)。

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

+0

在Obj-C函數被調用,「方法」我相當肯定,這可能是他爲什麼措辭的問題,「嵌套方法」 – NpC0mpl3t3 2016-07-16 13:04:28

+0

@ NpC0mpl3t3:我沒有看到任何地方的單詞「嵌套」或「方法」 OP的問題? – 2018-02-01 10:12:38

7

默認情況下不允許的Xcode嵌套函數。

如果要打開它們,請打開項目的Info,轉到Build選項卡,然後將「Other C flags」(在標題爲「GCC 4.2 - Language」的部分下)設置爲「-fnested-功能」。

(這是存儲在您的project.pbxproj文件「OTHER_CFLAGS =‘-fnested-功能’;」

+0

+1有關啓用的說明 – Moshe 2010-03-17 14:38:33

+0

請注意,這隻適用於C,我相信 - 不是Objective-C或C++。 – 2010-03-17 15:42:38

+1

@Paul你可以在Objective-C方法中嵌套一個C函數。你不能在另一個方法中嵌套一個方法,但這是有道理的。 Objective-C是一個非常簡單的C(或C++)頂級運行時。 – 2010-03-17 19:00:01

9

有點晚了,但你可以把內聯塊到一個函數,它有點像行爲您的嵌套函數的問題。

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

這是一個有點亂,但它的作品!

0

@Moshe你不能真正提供客觀C.內部嵌套函數相反,您可以使用該功能在最新的斯威夫特3,啓用此功能。它將如下所示:

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

用對象參數擴展由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]; 
    } 
} 

下面是該代碼的輸出:

code output