SwiftUI Mac 앱에서 임베디드 바이너리를 실행하는 방법은 무엇입니까?

Aug 19 2020

XIDEL을 기반으로하는 정말 간단한 XML 유효성 검사기를 개발하려고합니다.

지금까지 포함 된 XIDEL 바이너리를 실행하는 SwiftUI 코드가 있지만 유효성을 검사해야하는 XML을 전달하는 방법을 알 수 없습니다. 내 목표는 내 컴퓨터에서 XML 파일을 선택하고 내 앱 내의 콘텐츠보기에 XIDEL 결과를 표시하는 것입니다.

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)
}

}

답변

1 Asperi Aug 19 2020 at 18:05

다음을 시도하십시오

    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]