Looking up a NSString constant at runtime

2010-01-04 Thread David Alter
Is there a way to lookup what and NString constant is at runtime? I want to
know what the string is for a given constant. For example I would like to
pass in the constant name ( i.e. NSDeviceResolution) and get back
the NSString that constant represents. I know in this case that the Constant
name and the string are the same but I suspect that is not always true. Any
ideas?

thanks
-dave
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Looking up a NSString constant at runtime

2010-01-04 Thread Oftenwrong Soong
On Mon, Jan 4, 2010 at 4:56 PM, David Alter alterconsult...@gmail.com wrote:
 Is there a way to lookup what and NString constant is at runtime?



Have you tried

[NSDeviceResolution description]

or

[NSString stringWithString:NSDeviceResolution]

?

Soong


  
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Looking up a NSString constant at runtime

2010-01-04 Thread Scott Ribe
I think you're confused: the constant *is* the string; there is no lookup to
perform. You can do anything with it that you would do with any other
non-mutable string: log it, setStringValue on a text field in the user
interface, setMessageText in an alert, and so on.

-- 
Scott Ribe
scott_r...@killerbytes.com
http://www.killerbytes.com/
(303) 722-0567 voice


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Looking up a NSString constant at runtime

2010-01-04 Thread David Alter
This would work for finding out what the name is as well as logging it.

What if I'm getting a string passed in that is the name of the constant and
I want to return the constants string value. Is there a way to do that?

something like...
NSString * constValue = [SomeToolToLookupConstants constant:@
NSDeviceResolution'];

any idea?
-dave


On Mon, Jan 4, 2010 at 2:19 PM, Oftenwrong Soong
oftenwrongso...@yahoo.comwrote:

 On Mon, Jan 4, 2010 at 4:56 PM, David Alter alterconsult...@gmail.com
 wrote:
  Is there a way to lookup what and NString constant is at runtime?



 Have you tried

 [NSDeviceResolution description]

 or

 [NSString stringWithString:NSDeviceResolution]

 ?

 Soong



 ___

 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

 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:

 http://lists.apple.com/mailman/options/cocoa-dev/alterconsulting%40gmail.com

 This email sent to alterconsult...@gmail.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Looking up a NSString constant at runtime

2010-01-04 Thread Nick Paulson
The constant *is* an NSString;  essentially you could do:

NSString *constValue = NSDeviceResolution;

Though, that may be a little redundant.

--Nick Paulson
On Jan 4, 2010, at 6:09 PM, David Alter wrote:

 This would work for finding out what the name is as well as logging it.
 
 What if I'm getting a string passed in that is the name of the constant and
 I want to return the constants string value. Is there a way to do that?
 
 something like...
 NSString * constValue = [SomeToolToLookupConstants constant:@
 NSDeviceResolution'];
 
 any idea?
 -dave
 
 
 On Mon, Jan 4, 2010 at 2:19 PM, Oftenwrong Soong
 oftenwrongso...@yahoo.comwrote:
 
 On Mon, Jan 4, 2010 at 4:56 PM, David Alter alterconsult...@gmail.com
 wrote:
 Is there a way to lookup what and NString constant is at runtime?
 
 
 
 Have you tried
 
 [NSDeviceResolution description]
 
 or
 
 [NSString stringWithString:NSDeviceResolution]
 
 ?
 
 Soong
 
 
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 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:
 
 http://lists.apple.com/mailman/options/cocoa-dev/alterconsulting%40gmail.com
 
 This email sent to alterconsult...@gmail.com
 
 ___
 
 Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
 
 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:
 http://lists.apple.com/mailman/options/cocoa-dev/cocoa%40nickpaulson.com
 
 This email sent to co...@nickpaulson.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Looking up a NSString constant at runtime

2010-01-04 Thread Scott Ribe
 What if I'm getting a string passed in that is the name of the constant and
 I want to return the constants string value. Is there a way to do that?

This is C, and just as with variables, the names are not there at runtime.
If you really need to do this, you'll have to build your own lookup table,
possibly using some macro magic to avoid having to type the name twice for
each entry.

-- 
Scott Ribe
scott_r...@killerbytes.com
http://www.killerbytes.com/
(303) 722-0567 voice


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Looking up a NSString constant at runtime

2010-01-04 Thread David Alter
I can open a library and lookup a function by name using dlsym. These
constants are EXTERN. It seams there should be away to look these up as
well.

-dave


On Mon, Jan 4, 2010 at 3:39 PM, Scott Ribe scott_r...@killerbytes.comwrote:

  What if I'm getting a string passed in that is the name of the constant
 and
  I want to return the constants string value. Is there a way to do that?

 This is C, and just as with variables, the names are not there at runtime.
 If you really need to do this, you'll have to build your own lookup table,
 possibly using some macro magic to avoid having to type the name twice for
 each entry.

 --
 Scott Ribe
 scott_r...@killerbytes.com
 http://www.killerbytes.com/
 (303) 722-0567 voice



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Looking up a NSString constant at runtime

2010-01-04 Thread Graham Cox

On 05/01/2010, at 11:03 AM, David Alter wrote:

 I can open a library and lookup a function by name using dlsym. These
 constants are EXTERN. It seams there should be away to look these up as
 well.

Functions are not the same, because a function's name is a necessary part of 
the runtime. A constant's name is just a convenience for the programmer and 
doesn't make it into the binary.

There is no built-in standard way to do this - you're on your own.

--Graham


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Looking up a NSString constant at runtime

2010-01-04 Thread Mark Ritchie
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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Looking up a NSString constant at runtime

2010-01-04 Thread Scott Ribe
Ah yes, external symbols in a dynamic library--you do have some chance of
looking them up at run time ;-)

-- 
Scott Ribe
scott_r...@killerbytes.com
http://www.killerbytes.com/
(303) 722-0567 voice


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com