Objective C - 为什么常量以 k 开头

Objective C - Why do constants start with k(Objective C - 为什么常量以 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 开头

基础教程推荐