Re: Looking up a NSString constant at runtime
Re: Looking up a NSString constant at runtime
- Subject: Re: Looking up a NSString constant at runtime
- From: Mark Ritchie <email@hidden>
- Date: Mon, 04 Jan 2010 18:27:13 -0800
On 4/Jan/2010, at 4:24 PM, glenn andreas wrote:
> CFBundle has routines for looking up both functions and data by name. It does require you figure out what framework the symbol comes from (and then get the corresponding CFBundle), but it is doable.
Cool, I didn't know that CFBundle exposed access to external symbols at run time! I've always used nm (or some such) to look at the symbol table. This was a fun exercise to work out how to get access to the value of the symbol at runtime. Apologies if I've done something weird, I don't work in CF very often.
If you know the bundle identifier, then it would seem that this code will find the symbol and allow access to its value:
CFBundleRef bundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.AppKit"));
if(bundle){
NSString **pointer = (NSString **)CFBundleGetDataPointerForName (bundle, CFSTR("NSDeviceResolution"));
if(pointer){
NSLog(@"NSDeviceResolution = %@", *pointer);
}
}
If you don't know the bundle identifier then I assume you have to iterate over the available bundles (left as an exercise for someone else. ;-)
As to the usefulness of this, I can't say. The original poster never said why they were trying to do so I don't know if this will be suitable for the task... Today however, at this moment, it worked for me!
M.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden