从xcode运行时,使用NSTask的Swift 2.1 OSx Shell命令可以运行,但在导出时却不能运行 [英] Swift 2.1 OSx shell commands using NSTask work when run from xcode, but not when exported

查看:162
本文介绍了从xcode运行时,使用NSTask的Swift 2.1 OSx Shell命令可以运行,但在导出时却不能运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的OSx(10.11)应用程序,以在按下按钮时执行shell命令.当我从xcode运行它时,它可以工作,但是当我通过存档"导出应用程序时,其中一个按钮不再起作用.我没有收到错误,也没有任何输出.我使用的是绝对路径,所以我不明白为什么它可以在xcode中工作,但不能作为导出的应用程序使用,也不知道为什么一个按钮可以工作而另一个按钮不能工作.

I wrote a simple OSx (10.11) application to execute shell commands when a button is pressed. It works when I run it from xcode, but when I export the application via "archive", one of the buttons no longer works. I don't get an error and I don't get any output either. I am using absolute paths so I don't understand why it works in xcode but not as an exported application, Nor do I understand why one button works and the other doesn't.

这是我使用的make shell命令的主要功能

Here is the main function that I am using the make the shell commands

    func runCommand(path : String, args : [String]) -> (output: [String], error: [String], exitCode: Int32) {
    var output : [String] = []
    var error : [String] = []

    let task = NSTask()
    task.launchPath = path
    task.arguments = args

    let outpipe = NSPipe()
    task.standardOutput = outpipe
    let errpipe = NSPipe()
    task.standardError = errpipe

    task.launch()

    let outdata = outpipe.fileHandleForReading.readDataToEndOfFile()
    if var string = String.fromCString(UnsafePointer(outdata.bytes)) {
        string = string.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
        output = string.componentsSeparatedByString("\n")
    }

    let errdata = errpipe.fileHandleForReading.readDataToEndOfFile()
    if var string = String.fromCString(UnsafePointer(errdata.bytes)) {
        string = string.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
        error = string.componentsSeparatedByString("\n")
    }

    //task.waitUntilExit()
    let status = task.terminationStatus

    return (output, error, status)
}

这是起作用的按钮:

      // Check for configurator 2 app installation
    let (output, error, status) = self.runCommand("/bin/bash", args:  ["-c", "/bin/ls", "/Applications/Apple Configurator 2.app"])

这是没有的按钮:

        // Check if the phone is plugged in and paired
    let (output, error, status) = self.runCommand("/bin/bash", args: ["-c", "/usr/local/bin/cfgutil", "get", "isPaired"])

更奇怪的是,我发现(通过无奈)如果我反复单击不起作用的按钮,有时它最终会起作用.

What is even more strange, I discovered (through sheer frustration) that if I repeatedly click the button that doesn't work, it will sometimes eventually work.

推荐答案

您的问题是两件事同时发生的结果:

Your issue is the result of two things happening together:

  • 您返回默认值

  • you return default values

您没有为控制流指定替代分支

you don't specify alternative branches for the control flow

发生的事情是,它隐藏了潜在的故障,并导致代码难以调试,如您所经历的那样.

What happens is that it hides potential failures, and leads to code that is very hard to debug, as you experienced.

现有代码可能的解决方案是涵盖所有可能的方式,这意味着为if var string = String.fromCString(UnsafePointer(errdata.bytes))条件提供else分支,您将在其中处理错误.

A possible solution with your existing code is to cover all possible ways, meaning providing else branches to your if var string = String.fromCString(UnsafePointer(errdata.bytes)) conditions, where you will handle errors.

这篇关于从xcode运行时,使用NSTask的Swift 2.1 OSx Shell命令可以运行,但在导出时却不能运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆