Popeye
Popeye

Reputation: 12093

Can I do NSVariableFromString like NSClassFromString and NSSelectorFromString?

Right so I have noticed that you can do NSClassFromString and NSSelectorFromString.

Is it possible to do something like NSVariableFromString?

Upvotes: 1

Views: 517

Answers (3)

Jody Hagins
Jody Hagins

Reputation: 28409

The objective C runtime has lots of goodies for your consumption.

If you want an iVar, you can call object_getInstanceVariable with a string name.

If you want variables, it's a bit more work, and they have to be globally visible to the linker. You can use CFBundleGetDataPointerForName for that purpose.

Be sure to read the documentation for restrictions and specific information about runtime information, and the availability of stuff on different platforms.

Upvotes: 3

Stephen Darlington
Stephen Darlington

Reputation: 52565

You can get values from strings using NSScanner but, as JemeryP notes, at runtime variable names have generally been converted to pointers and memory addresses.

Upvotes: 1

JeremyP
JeremyP

Reputation: 86651

No. Compiled applications don't contain variable names except when debug info is included and it usually isn't for release applications.

Upvotes: 4

Related Questions