Is the a ScriptEngine or eval()-like funct

2019-07-16 02:57发布

问题:

In Java, we can build up expressions to be called using ScriptEngine. This is nice for building up frameworks based on a common naming convention. In JavaScript, there is of course eval(). Does Swift have some sort of mechanism for evaluating a string which contains a swift expression? I'm aware that this could be potentially abused; however, it would simplify my present development.

回答1:

No. Swift is a compiled language, and the runtime doesn't include the compiler. The iOS SDK doesn't provide a way to evaluate run-time Swift code.

You can execute JavaScript using JavaScriptCore, and JavaScriptCore makes it pretty easy to expose Swift objects and functions to the script. Maybe that will help you.



标签: ios swift eval