gpt4 book ai didi

ios - 如何通过名称获取 "extern const int"值

转载 作者:行者123 更新时间:2023-11-29 02:04:28 46 4
gpt4 key购买 nike

我想通过名称获取 extern constint 值。

例如在我的 .h 文件中:

extern const int MY_INT_CONST;

在我的 .m 文件中:

const int MY_INT_CONST = 0;

我想要的:

- (void) method {
int i = [getMyConstantFromString:@"MY_INT_CONST"];
}

我该怎么做?

我在 RunTime api 中搜索,但没有找到任何内容。

最佳答案

没有简单的方法可以做到这一点。语言和运行时都没有为此提供便利。

可以使用动态加载器的 API 通过名称查找符号的地址。

// Near top of file
#include <dlfcn.h>

// elsewhere
int* pointer = dlsym(RTLD_SELF, "MY_INT_CONST");
if (pointer)
{
int value = *pointer;
// use value...
}

请注意,这是传递给 dlsym() 的 C 风格字符串。如果你有一个 NSString,你可以使用 -UTF8String 来获得一个 C 风格的字符串。

关于ios - 如何通过名称获取 "extern const int"值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29936853/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com