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

相关问题