Execute external swift code inside a macOS app

Viewed 80

I have an external swift 5 file (just function code no UI) that will be fetched daily from a server. I want to embed the code in my running MacOS app so that the code in the external swift file will be executed when the user presses a button. I´m not sure if this is even possible.

The Idea is something like this:

External swift script:

import SwiftUI

func sayHello() {

print("Hi")

}

func newCode() {

// some fancy code

}

The script contains one or two functions (always the same name) that should be called from the buttons (just for simplicity). The code inside the functions will be always completely different e.g.: some processing of data or changing settings in my app via the app api.

It would be great if there might be a swift solution, other ways I must use a shell script that is executed with Pipe() but I´m not that good in shell programming so I want to avoid it.

The Apple Shortcut app allows user to "program" their code by drag n drop, so maybe this is way to do so, but I don't no how their logic behind this works and if this is similar to my problem.

0 Answers
Related