Can I do NSVariableFromString like NSClassFromStri

2020-03-30 07:58发布

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

Is it possible to do something like NSVariableFromString?

3条回答
萌系小妹纸
2楼-- · 2020-03-30 08:23

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

查看更多
仙女界的扛把子
3楼-- · 2020-03-30 08:37

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.

查看更多
Deceive 欺骗
4楼-- · 2020-03-30 08:42

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.

查看更多
登录 后发表回答