简体   繁体   English

为什么不能将常量放入CGRectMake?

[英]Why can't I put a constant into an CGRectMake?

I have tried this: 我已经试过了:

CGRectMake(0.0f, kFooBarHeight, 100.0f, 10.0f);

I get an error unexpected ';' before ')' 我收到unexpected ';' before ')'的错误unexpected ';' before ')' unexpected ';' before ')' , and too few arguments for CGRectMake . unexpected ';' before ')' ,并且too few arguments for CGRectMake When I exchange this with: 当我与以下人员交换时:

CGFloat foo = kFooBarHeight;
CGRectMake(0.0f, foo, 100.0f, 10.0f);

then all is fine. 那一切都很好。 Are constants not suitable to pass along as parameters? 常量不适合作为参数传递吗?

Without the kFooBarHeight definition it's impossible to give a good answer but I'm guessing you defined kFooBarHeight using a preprocessor definition? 如果没有kFooBarHeight定义,就不可能给出一个很好的答案,但是我猜您是使用预处理器定义来定义kFooBarHeight的吗? If so, best guess is you added a semicolon to the end. 如果是这样,最好的猜测是您在末尾添加了分号。 Your definition should look like this: #define kFooBarHeight 10 but you have set as: #define kFooBarHeight 10; 您的定义应如下所示: #define kFooBarHeight 10但已设置为: #define kFooBarHeight 10; .

If what you have is the second definition when it replaced by the preprocessor you get: 如果您拥有的是第二个定义,当它被预处理器替换时,您将得到:

CGRectMake(0.0f, 10;, 100.0f, 10.0f);

That's why your second example works correctly, it expands to: 这就是您的第二个示例正确运行的原因,它扩展为:

CGFloat foo = 10;;
CGRectMake(0.0f, foo, 100.0f, 10.0f);

Again, this is just an educated guess, it's impossible to say without the actual definition of kFooBarHeight. 同样,这只是一个有根据的猜测,没有kFooBarHeight的实际定义就不可能说。

Change your 改变你的

#define kFooBarHeight 100;

to

#define kFooBarHeight 100

Semicolons should not be used to terminate #defines unless you know for certain how it will be used. 除非您确定如何使用分号,否则不应使用分号来终止#define。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM