2011-10-24 115 views
0

這真的很簡單,但讓我瘋狂。Obj-C函數聲明需要分號?

我想在我的Objective-C代碼中實現一個簡單的函數。當我寫這個,

NSInteger Sort_Function(id id1, id id2, void *context) { 

} 

我得到一個錯誤,在分析結束時預計分號。不過,我已經在許多例子中看到了這種類型的語法。我可能會做錯什麼?如果它很重要,這是一個iOS應用程序,並且該函數嵌套在if子句中。提前致謝。

+1

請添加一些周圍的代碼。很難說出這裏發生了什麼。 –

回答

6

函數定義 - 您發佈的這個片段 - 是「嵌套在if條款」?不幸的是,C(和Obj-C通過擴展) - 所有的函數聲明和定義必須位於文件的頂層。內部的@implementation部分也是一種選擇:

// Start of file 

// Declaration or definition valid here 
void my_func(void); // Declaration 
void my_other_func(void); 
void my_third_func(void); 

void my_func(void) { // Definition 
    return; 
} 

@implementation MyClass 

// Definition also valid here 
void my_other_func(void) { 
    return; 
} 

- (void) myMethod { 
    if(YES){ 
     void my_third_func(void) { // Invalid here 
      return; 
     } 
    } 
} 

@end 

是否有可能你混淆了block syntax函數語法?

// The caret indicates definition of a block, sort of an anonymous function 
int (^myBlock)(int); 
myBlock = ^int (int my_int) { 
    return my_int; 
}; 
+0

這確實是if條款的結果。將它移動到實現塊似乎解決了分號問題。謝謝! – Rob

+0

很高興我能幫到你。 –