#define vs const in Objective-C(Objective-C 中的#define vs const)
问题描述
I'm new to Objective-C, and I have a few questions regarding const
and the preprocessing directive #define
.
First, I found that it's not possible to define the type of the constant using #define
. Why is that?
Second, are there any advantages to use one of them over the another one?
Finally, which way is more efficient and/or more secure?
First, I found that its not possible to define the type of the constant using #define, why is that?
Why is what? It's not true:
#define MY_INT_CONSTANT ((int) 12345)
Second, are there any advantages to use one of them over the another one?
Yes. #define
defines a macro which is replaced even before compilation starts. const
merely modifies a variable so that the compiler will flag an error if you try to change it. There are contexts in which you can use a #define
but you can't use a const
(although I'm struggling to find one using the latest clang). In theory, a const
takes up space in the executable and requires a reference to memory, but in practice this is insignificant and may be optimised away by the compiler.
const
s are much more compiler and debugger friendly than #define
s. In most cases, this is the overriding point you should consider when making a decision on which one to use.
Just thought of a context in which you can use #define
but not const
. If you have a constant that you want to use in lots of .c
files, with a #define
you just stick it in a header. With a const
you have to have a definition in a C file and
// in a C file
const int MY_INT_CONST = 12345;
// in a header
extern const int MY_INT_CONST;
in a header. MY_INT_CONST
can't be used as the size of a static or global scope array in any C file except the one it is defined in.
However, for integer constants you can use an enum
. In fact that is what Apple does almost invariably. This has all the advantages of both #define
s and const
s but only works for integer constants.
// In a header
enum
{
MY_INT_CONST = 12345,
};
Finally, which way is more efficient and/or more secure?
#define
is more efficient in theory although, as I said, modern compilers probably ensure there is little difference. #define
is more secure in that it is always a compiler error to try to assign to it
#define FOO 5
// ....
FOO = 6; // Always a syntax error
const
s can be tricked into being assigned to although the compiler might issue warnings:
const int FOO = 5;
// ...
(int) FOO = 6; // Can make this compile
Depending on the platform, the assignment might still fail at run time if the constant is placed in a read only segment and it's officially undefined behaviour according to the C standard.
Personally, for integer constants, I always use enum
s for constants of other types, I use const
unless I have a very good reason not to.
这篇关于Objective-C 中的#define vs const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Objective-C 中的#define vs const


基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01