在 Objective-C 表达式中使用常量变量?
爪哇语
静态最终 int VCount = 21,TCount = 28,NCount = VCount * TCount;
Objective-C 中的
static int VCount = 21,TCount = 28,NCount = ???;
如何表达NCount int,因为它引用变量?
In Java
static final int VCount = 21, TCount = 28, NCount = VCount * TCount;
in Objective-C
static int VCount = 21, TCount = 28, NCount = ???;
How can I express the NCount int because it refers to variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些变量不是常量。 通常在 Objective-C 中,您可以使用预处理器定义基本常量并将它们放置在共享头文件中。 例如:
以这种方式创建的常量通常全部大写。 另一种约定是在共享源文件(所有内容都链接到的文件)中创建符号并将其声明为外部符号。 Cocoa 通过明确定义的键值来实现这一点。 例如,在共享头文件中,您可以定义变量:
然后在某个源文件中,定义变量的实际值(在文件范围内):
如果您的类只是使用全局常量变量,并且该值不需要类之外的范围,可以使用这些技术中的任何一种,并且只需不在任何共享头文件中提供它们的定义即可。
Those variable's are not constants. Usually in Objective-C you would define basic constants using the pre-processor and place them in shared header files. For example:
Constants created in this way are typically written in all caps. Another convention is to create the symbol in a shared source file (one that everything is linked to) and declare it as an external symbol. Cocoa does this a lit with well-defined key values. For example, in the shared header file, you would define the variable:
Then in some source file, you define the actual value of the variable (at the file scope):
If your class is simply using a global, constant variable and the value needs no scope outside your class, it is okay to use any of these techniques and simply not provide definitions for them in any shared header file.