Is the a ScriptEngine or eval()-like function in Swift?

余生颓废 提交于 2019-12-24 02:25:37

问题


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.



来源:https://stackoverflow.com/questions/26244069/is-the-a-scriptengine-or-eval-like-function-in-swift

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