问题描述
在Java中,我们可以构建要使用ScriptEngine
调用的表达式.这对于基于通用命名约定构建框架非常有用.在JavaScript中,当然有eval()
. Swift是否具有某种机制来评估包含swift表达式的字符串?我知道这可能会被滥用;但是,这将简化我目前的开发.
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.
推荐答案
否. Swift是一种编译语言,运行时不包括编译器. iOS SDK没有提供评估运行时Swift代码的方法.
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.
您可以使用JavaScriptCore执行JavaScript,而JavaScriptCore使向脚本公开Swift对象和函数变得非常容易.也许会对您有所帮助.
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.
这篇关于是Swift中的ScriptEngine或eval()类函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!