2010-10-26 25 views
0

我想有一個枚舉作爲我的函數的參數。這會工作嗎?是否有可能使用像UITableViewCellStyle這樣的枚舉作爲方法的參數?

(UIFont*) myMethodName:(UITableViewCellStyle) cellStyle { 
    //... 
    if (cellStyle == UITableViewCellStyleValue2) 
     // ... 
} 

然後我會打電話像這樣

UIFont *resultFont = [self myMethodName:UITableViewCellStyleSubtitle]; 

只有下面的參數應該被允許的方法: UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle

這可能嗎?

回答

3
  • 將這項工作? →是

  • 只有下面的參數應該被允許: →不,它是不可能限制輸入於這些值,即

    UIFont *resultFont = [self myMethodName:12345]; 
    

    仍將編譯(假設你沒有使用目標C++)。

2

肯定的:

typedef enum _MyType { 
    type_a = -1, 
    type_b = 0, 
    type_c = 1, 
} MyType; 

... 

- (void) someMethod:(MyType)type { 
    if (type == type_a) ... 
} 
0

是的,這是可能的。

(這感覺就像一個不必要的簡單的答案,但我想不出什麼要補充!)

+0

這就是我想知道的;) – testing 2010-10-26 09:23:49

相關問題