【问题标题】:How to run an embedded binary in SwiftUI Mac app?如何在 SwiftUI Mac 应用程序中运行嵌入式二进制文件?
【发布时间】:2020-08-19 10:54:36
【问题描述】:

我正在尝试基于 XIDEL 开发一个非常简单的 XML 验证器。

到目前为止,这是我的 SwiftUI 代码,它执行嵌入式 XIDEL 二进制文件,但我不知道如何传递应该验证的 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)
}

}

【问题讨论】:

    标签: xcode macos swiftui embedded-binary


    【解决方案1】:

    试试下面的

        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]
    

    【讨论】:

    • 还有一种方法可以做一种 UNIX 管道吗?就像这样 /usr/local/bin/xidel -e="//recipe/flavor1/text() my.xml | awk '!seen[$0]++' | awk '!/NA/'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多