eval in Objective-C [duplicate]

扶醉桌前 提交于 2019-11-28 06:24:08

问题


Possible Duplicate:
Is there something like Matlab's eval statement in Objective-C 2.0?

Is there an eval function (as in lisp, javascript, python, ruby...) in Objective-C?

What I mean by eval is a function that can take in arbitrary Objective-C code (possibly having class definitions, side effects, instrospection, etc) as a string (not a block or an NSInvocation or IMP or something like that), and evaluate each expression, respecting the current runtime environment state, side effects, class definitions, etc.

If not, is it possible to implement within the confines of the existing runtime?


回答1:


Neither the language nor Apple's frameworks directly support such a thing. However, one of the goals of LLVM is to be an embeddable compiler suite. I'm pretty sure it can generate executable code right into memory. The hard part would be providing that code with access to the pre-existing environment of the calling code. For example, compiling code which references a local variable or something like that.

(Mind you, this approach is forbidden for the iOS App Store, but it could maybe be workable on Mac OS X.)




回答2:


Absolutely not. Objective-C is a fully compiled language. Only interpreted languages can do that sort of thing.




回答3:


No. Code eval is the feature for dynamic language. Although objective-C has dynamic feature , and even Cocoa runtime , it is still considered as a static language (generally).



来源:https://stackoverflow.com/questions/10158036/eval-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!