eval in Objective-C [duplicate]

2019-01-27 04:35发布

问题:

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).