#define vs const并链接到框架 [英] #define vs const and linking against frameworks
问题描述
我有一个非常小的轻量级应用程序,它需要使用一些存储在较大框架中的常量.我不想复制这些常量并将其硬编码到轻量级应用程序中,但是我也不想链接到大型框架来仅获取常量.
I have a really small lightweight application which needs to use some constants that are stored in a larger framework. I don't want to duplicate these constants and hardcode them into the lightweight application but I also don't want to have to link against the large framework to just get the constants.
在头文件中使用static NSString *const
定义常量.用#define
替换static NSString *const
是否可以防止我不得不链接整个框架?
The constants are defined using static NSString *const
in a header file. Does replacing the static NSString *const
with #define
prevent me from having to link against the whole framework ?
说实话,我不太确定链接的工作方式,所以我可能会错误地考虑这个问题
To be honest, I'm not entirely sure how linking works so I'm probably thinking about this incorrectly
推荐答案
是的,如果您#define常量,则只需要#import包含它们的.h文件.
Yes, if you #define the constants you just need to #import the .h file that contains them.
您确实需要注意,#defined常量是文字文本替换-像静态const值一样,它们没有类型"等.
You do need to be aware that the #defined constants are literal text substitutions -- they have no "type", etc, as static const values would.
但是另一个选择(仅适用于整数常量)是定义 C样式枚举在.h文件中.
But another option (for integer constants only) is to define C-style enums in a .h file.
这篇关于#define vs const并链接到框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!