const是在CGFloat之前还是之后?

ma1*_*w28 15 c floating-point const objective-c cgfloat

它甚至重要吗?Const之前还是const之后?我猜测我是constCGFloat它之前还是之后使它的值CGFloat变为常量,但指针呢?这是否适合Objective-C:

// Example.h

extern CGFloat const kPasscodeInputBoxWidth;


// Example.m

CGFloat const kPasscodeInputBoxWidth = 61.0f;
Run Code Online (Sandbox Code Playgroud)

Jer*_*fin 22

它可以在之前或之后进行.在指针的情况下,重要的是const在星号之前或之后结束:

const int *a;    // pointer to const int -- can't change what a points at
int const *a;    // same

int *const a;    // const pointer to int -- can't change the pointer itself.
                 // Note: must be initialized, since it can't be assigned.
Run Code Online (Sandbox Code Playgroud)

  • 你也可以使用`const int*const a;`:一个只读对象指向只读对象:) (4认同)
  • 问题涉及CGFloat而不是整数指针.对于整数,Apple指南声明"如果常量与其他常量无关,则可以使用const创建整数常量;否则,使用枚举.".他们在使用float时只提倡const.请参阅https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.pdf (2认同)

Gus*_*son 5

没关系(我一直使用前者,但我想这是风格问题):

const CGFloat kPasscodeInputBoxWidth = 61.0;
CGFloat const kPasscodeInputBoxWidth = 61.0;
Run Code Online (Sandbox Code Playgroud)

至少在当前的演绎中CGFloat,它只是一个typedef double,就像你使用常规的原始数据类型一样.对于指针,const的放置将决定它是指针还是常量值,因此它确实很重要.