How do I lookup a string constant at runtime in Objective-C?

后端 未结 1 921
抹茶落季
抹茶落季 2020-11-29 12:28

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

相关标签:
1条回答
  • 2020-11-29 13:04

    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);
    }
    

    Example use:

    NSString *version = lookupStringConstant(@"VungleSDKVersion");
    NSLog(@"Version = %@",version);
    
    0 讨论(0)
提交回复
热议问题