First of all, I know that Apple rejects apps that include something like that. It's just for the personal purpose.
I tried the following example:
import Foundation
var instructions: String = "let str = \"Hello\"\nprint(\"\\(str) world\")\n"
let process: Process = Process()
process.launchPath = "/usr/bin/swift"
let inpipe: Pipe = Pipe()
let outpipe: Pipe = Pipe()
inpipe.fileHandleForWriting.write(instructions.data(using: String.Encoding.utf8, allowLossyConversion: true)!)
process.standardInput = inpipe
process.standardOutput = outpipe
process.launch()
process.waitUntilExit() // never ends
process.standardInput = Pipe()
let data = outpipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: String.Encoding.utf8)
I run the program on a Mac. The program never ends. I don't know where the error could be.