Come eseguire un file binario incorporato nell'app SwiftUI per Mac?

Aug 19 2020

Sto cercando di sviluppare un validatore XML davvero semplice basato su XIDEL.

Ecco il mio codice SwiftUI finora, che esegue il binario XIDEL incorporato, ma non riesco a capire come passare XML che dovrebbe essere convalidato. Il mio obiettivo è selezionare un file XML dal mio computer e mostrare i risultati di XIDEL in una visualizzazione del contenuto all'interno della mia app.

struct ContentView: View {

@State var message = "Hello, World!"
@State var isRunning = false

var body: some View {
    VStack {
        Text("XML Validator")
            .font(.largeTitle)
            .padding()
        HStack {
            TextField("Message", text: $message)
                .padding(.leading)
            Button(action: {

                let task = Process()
                let bundle = Bundle.main
                let execURL = bundle.url(forResource: "xidel", withExtension: nil)
                guard execURL != nil else {
                    print("XIDEL executable could not be found!")
                    return
                }
                task.executableURL = execURL!
                task.arguments = ["-e=//recipe/flavor1/text() my.xml"]
                do {
                    try task.run()
                    print("XIDEL executed successfully!")
                    self.isRunning = true
                } catch {
                    print("Error running XIDEL: \(error)")
                    self.isRunning = false
                }
                                    
            }) {
                Text("Validate")
            }.disabled(isRunning)
                .padding(.trailing)
        }
    }.frame(maxWidth: .infinity, maxHeight: .infinity)
}

}

Risposte

1 Asperi Aug 19 2020 at 18:05

Prova quanto segue

    guard let path = Bundle.main.path(forResource: "my", ofType: "xml") else {
       print("my.xml could not be found!")
       return 
    }
    task.arguments = ["-e=//recipe/flavor1/text()", path]