2011-05-18 77 views
0
@implementation classname 

static const unsigned int OFFSET_STX =0; 
static const unsigned int OFFSET_ETX =1; 
static const unsigned int OFFSET_KTX =2; 
static const unsigned int OFFSET_MTX =4; 
static const unsigned int OFFSET_LTX =5; 

static const char STX =0x05; 
static const char ETX =0x09; 

@end 

錯誤 '__attribute__':預計 '=', '', '', 'ASM' 或 '之前的sizeof'

expected '=', ',', ';', 'asm' or '__attribute__' before 'sizeof' 

我如何聲明類裏面這些靜態變量。

我是否需要申報

+(int)OFFSET_ETX 
{ 
return OFFSET_ETX=0; 
} 

,並通過調用[類名OFFSET_ETX]爲每個靜態變量。 我有超過10個靜態變量要分配在我的程序中。

+0

[錯誤的可能重複:預期'',',',';','asm'或'__attribute__'''foo']之前(http://stackoverflow.com/questions/990906/iphone-error-expected-asm-or-attribute- before-foo)或[Objective-C錯誤預期asm或屬性之前的類](http://stackoverflow.com/questions/1825597/) – 2011-05-18 03:41:50

+0

@Josh Caswell:我的問題不同於這兩個鏈接。 – spandana 2011-05-18 04:14:04

回答

3

你不能把一個靜態變量,類接口內的目標C.在Objective C,static具有相同的含義,因爲它在C.確實做到這一點,而不是:

enum { 
    OFFSET_STX = 0, 
    OFFSET_ETX = 1, 
    OFFSET_KTX = 2, 
    OFFSET_MTX = 3, 
    OFFSET_LTX = 4 
}; 

@implementation classname 
... 
@end 
+0

它的靜態常量unsigned int.Will它會導致任何問題,因爲枚舉將保存const int而不是unsigned int,我也有5個和更多的聲明像這樣「靜態常量字符STX = 0x01」。它也向我顯示相同的錯誤我不能在enum裏面聲明。 – spandana 2011-05-18 04:17:54

+2

靜態變量只能出現在文件範圍或函數內部。你不能把它們放在類,結構體或枚舉中。 '0x01'是一個非常好的值來放入一個枚舉 - 當你使用它時,一個'char'常量將被上轉換爲'int'或'unsigned int'。 – 2011-05-18 04:26:31

相關問題