Objective C - Why do constants start with k(Objective C - 为什么常量以 k 开头)
问题描述
为什么我见过的所有例子中的常量总是以 k 开头?我应该在头文件或 .m 文件中#define 常量吗?
Why do constants in all examples I've seen always start with k? And should I #define constants in header or .m file?
我是 Objective C 的新手,我不知道 C.是否有一些教程可以在不假设 C 知识的情况下解释这些事情?
I'm new to Objective C, and I don't know C. Is there some tutorial somewhere that explains these sorts of things without assuming knowledge of C?
推荐答案
以k"开头的常量是 Mac OS X 之前的遗留问题.事实上,我认为这种做法甚至可能来自当时的方式,当时 Mac OS 主要是用 Pascal 编写的,主要的开发语言是 Pascal.在 C 中,#define
的常量通常以全大写形式编写,而不是以k"为前缀.
Starting constants with a "k" is a legacy of the pre-Mac OS X days. In fact, I think the practice might even come from way back in the day, when the Mac OS was written mostly in Pascal, and the predominant development language was Pascal. In C, #define
'd constants are typically written in ALL CAPS, rather than prefixing with a "k".
至于在哪里#define
常量:#define
它们你将在哪里使用它们.如果您希望 #import
您的代码的人使用这些常量,请将它们放在头文件中;如果常量只在内部使用,请将它们放在 .m
文件中.
As for where to #define
constants: #define
them where you're going to use them. If you expect people who #import
your code to use the constants, put them in the header file; if the constants are only going to be used internally, put them in the .m
file.
这篇关于Objective C - 为什么常量以 k 开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Objective C - 为什么常量以 k 开头


- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01