如何在Objective-C中在运行时查找字符串常量? [英] How do I lookup a string constant at runtime in Objective-C?

查看:155
本文介绍了如何在Objective-C中在运行时查找字符串常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司开发了一个广告SDK,可以调解其他广告网络。在运行时,它使用 NSClassFromString 检查其他广告网络是否存在,如果它们存在则发送这些类消息。

My company develops an advertising SDK that mediates other ad networks. At runtime, it checks if the other ad networks are present by using NSClassFromString, and sends those classes messages if they're present.

这适用于Objective-C对象,但是如何在运行时加载字符串常量?在这种情况下,我想检查只能通过字符串常量提供的SDK版本( extern NSString * VungleSDKVersion;

This works fine for Objective-C objects, but how can I load a string constant at runtime? In this case, I want to check the version of an SDK that is only available through a string constant (extern NSString* VungleSDKVersion;)

推荐答案

您可以使用 CFBundleGetDataPointerForName 在运行时查找常量值

You can use CFBundleGetDataPointerForName to lookup a constant's value at runtime

NSString *lookupStringConstant(NSString *constantName) {
    void ** dataPtr = CFBundleGetDataPointerForName(CFBundleGetMainBundle(), (__bridge CFStringRef)constantName);
    return (__bridge NSString *)(dataPtr ? *dataPtr : nil);
}

使用示例:

NSString *version = lookupStringConstant(@"VungleSDKVersion");
NSLog(@"Version = %@",version);

这篇关于如何在Objective-C中在运行时查找字符串常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆