[Solved] Swift NStask function


Try this:

func commmand(argument: String) -> String
{

    let task:NSTask = NSTask()
    let pipe:NSPipe = NSPipe()

    task.launchPath = "/bin/menubar"
    task.arguments = [argument]
    task.standardOutput = pipe
    task.launch()

    let handle = pipe.fileHandleForReading
    let data = handle.readDataToEndOfFile()
    let result_s = String(data: data, encoding: NSUTF8StringEncoding)!
    return result_s
}

print(commmand("getip"))

2

solved Swift NStask function