如何动态设置静态“#define”头文件(.h)中的变量?

发布于 2024-11-09 05:42:12 字数 302 浏览 0 评论 0原文

我在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春风十里 2024-11-16 05:42:12

#define 始终在代码编译期间进行评估。您应该修改该库才能获得此功能。

另一种解决方案如下。使用方法创建一些类 Provider

+ (NSString *)getYourConstant;

并使用以下宏

#import "Provider.h"

#define kTheConstantYouNeed [Provider getYourConstant]

#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 a

+ (NSString *)getYourConstant;

method and use the following macro

#import "Provider.h"

#define kTheConstantYouNeed [Provider getYourConstant]
池予 2024-11-16 05:42:12

您可以使用 ifdef 宏预处理器在编译时重新定义变量。您无法“更改该变量”,因为它一开始就不是变量。这是一个宏,它将所有出现的 SHKMyAppName 替换为 @"My App Name

这是您可以执行的操作。定义一个编译器变量(您可以在 XCode 项目配置中执行此操作) ) 替换为您正在构建的项目名称,然后将 #define 替换为:

#if defined(PROJECT_APP_01_BUILD)
#define SHKMyAppName @"My App 01"
#elif defined(PROJECT_APP_02_BUILD)
#define SHKMyAppName @"My App 02"
#elif defined(PROJECT_APP_03_BUILD)
#define SHKMyAppName @"My App 03"
#endif

这样,当您编译 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 of SHKMyAppName 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:

#if defined(PROJECT_APP_01_BUILD)
#define SHKMyAppName @"My App 01"
#elif defined(PROJECT_APP_02_BUILD)
#define SHKMyAppName @"My App 02"
#elif defined(PROJECT_APP_03_BUILD)
#define SHKMyAppName @"My App 03"
#endif

That way, when you are compiling App 01, macro SHKMyAppName will be replaced by @"My App 01"'. When you are compiling **App 02**, macroSHKMyAppNamewill be replaced by@"My App 02"'. And so on, so forth.

黎歌 2024-11-16 05:42:12

您可以将这些定义移至单个 appinfo.h 文件并将其包含在其他标头中。

这也可能是获取版本信息的好地方。

例如

在 SHKConfig.h 中:

#include "MyAppInfo.h"
#define SHKMyAppName    MyAppName // define MyAppName elsewhere

在 MapAppInfo.h 中:

#define MyAppName "Bla bla"

或者...

在 SHKConfig.h 中:

#define SHKMyAppName    MyAppName // define MyAppName elsewhere

并在应用程序的编译器设置中添加:

-DMyAppName=\"Bla bla\"

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:

#include "MyAppInfo.h"
#define SHKMyAppName    MyAppName // define MyAppName elsewhere

And in MapAppInfo.h:

#define MyAppName "Bla bla"

Or...

In SHKConfig.h:

#define SHKMyAppName    MyAppName // define MyAppName elsewhere

And in app's compiler settings add:

-DMyAppName=\"Bla bla\"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文