[英]How do I lookup a string constant at runtime in Objective-C?
My company develops an advertising SDK that mediates other ad networks. 我公司开发了一个广告SDK,用于调解其他广告网络。 At runtime, it checks if the other ad networks are present by using
NSClassFromString
, and sends those classes messages if they're present. 在运行时,它使用
NSClassFromString
检查其他广告网络是否存在,并在它们存在时发送这些类消息。
This works fine for Objective-C objects, but how can I load a string constant at runtime? 这适用于Objective-C对象,但如何在运行时加载字符串常量? In this case, I want to check the version of an SDK that is only available through a string constant (
extern NSString* VungleSDKVersion;
) 在这种情况下,我想检查只能通过字符串常量提供的SDK版本(
extern NSString* VungleSDKVersion;
)
You can use CFBundleGetDataPointerForName
to lookup a constant's value at runtime 您可以使用
CFBundleGetDataPointerForName
在运行时查找常量的值
NSString *lookupStringConstant(NSString *constantName) {
void ** dataPtr = CFBundleGetDataPointerForName(CFBundleGetMainBundle(), (__bridge CFStringRef)constantName);
return (__bridge NSString *)(dataPtr ? *dataPtr : nil);
}
Example use: 使用示例:
NSString *version = lookupStringConstant(@"VungleSDKVersion");
NSLog(@"Version = %@",version);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.