As others noted, if you are creating an iOS application (especially for distribution in the app store), you cannot do this. However, if you are writing Swift code for an OS X machine And you know that Xcode is installed on your computer, you can run the Swift code line by running the Swift compiler from the command line. Something like this (with correct error checking, of course):
var str = "let str = \"Hello\"\nprintln(\"\\(str) world\")\n" let task = Process() task.launchPath = "/usr/bin/swift" let outpipe = Pipe() let inpipe = Pipe() inpipe.fileHandleForWriting.write(str.data(using: String.Encoding.utf8, allowLossyConversion: true)!) task.standardInput = inpipe task.standardOutput = outpipe task.launch() task.waitUntilExit() task.standardInput = Pipe() let data = outpipe.fileHandleForReading.readDataToEndOfFile() let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
Again, this is probably not recommended in almost all real cases, but it is the way you can execute the String of Swift code if you really need to.
source share