如何动态设置静态“#define”头文件(.h)中的变量?
我在我的 iOS 项目(sharekit)中使用开源组件,他们在头文件中定义了静态变量。例如,在SHKConfig.h
中,他们将应用程序名称定义为:
#define SHKMyAppName @"My App Name"
我想做的是使其动态化;我不想在该特定文件中对这些变量进行硬编码。由于头文件中显然没有 viewDidLoad 方法(据我所知),我怎么可能动态分配这个变量呢?
这可能吗?
提前非常感谢!
I am using an open source component in my iOS project (sharekit), and they defined static variables in the header file. For example, in SHKConfig.h
they define the App Name as:
#define SHKMyAppName @"My App Name"
What I would like to do is make this dynamic; I don't want to hard-code those variables in that specific file. Since there is obviously no viewDidLoad
method in a header file (as far as I know), how could I possibly dynamically assign this variable?
Is this even possible?
Thank you very much in advance!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
#define
始终在代码编译期间进行评估。您应该修改该库才能获得此功能。另一种解决方案如下。使用方法创建一些类
Provider
并使用以下宏
#define
is always evaluated during code compilation. You should modify the library to get this functionality.Another solution is the following. Create some class
Provider
with amethod and use the following macro
您可以使用 ifdef 宏预处理器在编译时重新定义变量。您无法“更改该变量”,因为它一开始就不是变量。这是一个宏,它将所有出现的
SHKMyAppName
替换为@"My App Name
。这是您可以执行的操作。定义一个编译器变量(您可以在 XCode 项目配置中执行此操作) ) 替换为您正在构建的项目名称,然后将
#define
替换为:这样,当您编译 App 01 时,宏
SHKMyAppName
将会出现。被替换为@"My App 01"' 当您编译**App 02**时,宏
SHKMyAppName将被替换为
@"My App 02"'。等等,等等。You could use
ifdef
macro preprocessor to redefine the variable at compile time. There is no way you could "change that variable" since it's not even a variable to begin with. It's a MACRO that replaces all occurrences ofSHKMyAppName
with@"My App Name
.Here's is what you could do. Define a compiler variable (you can do that in XCode project configurations) with the project name you are building. Then, replace
#define
with:That way, when you are compiling App 01, macro
SHKMyAppName
will be replaced by@"My App 01"'. When you are compiling **App 02**, macro
SHKMyAppNamewill be replaced by
@"My App 02"'. And so on, so forth.您可以将这些定义移至单个 appinfo.h 文件并将其包含在其他标头中。
这也可能是获取版本信息的好地方。
例如
在 SHKConfig.h 中:
在 MapAppInfo.h 中:
或者...
在 SHKConfig.h 中:
并在应用程序的编译器设置中添加:
You could move these defines to a single appinfo.h file and include it within other headers.
This might also be a good place for version info.
E.g.
In SHKConfig.h:
And in MapAppInfo.h:
Or...
In SHKConfig.h:
And in app's compiler settings add: