Ben Reed
Ben Reed

Reputation: 874

Is the a ScriptEngine or eval()-like function in 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.

Upvotes: 2

Views: 1538

Answers (1)

rob mayoff
rob mayoff

Reputation: 385860

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.

Upvotes: 5

Related Questions