Swift NTask 函数
Swift NStask function
我是个彻头彻尾的 swift 菜鸟。在 xcode 中使用此代码我得到了我需要的结果。我创建了一个带有多个参数的命令行二进制文件 "menubar"。我通常在终端“/bin/menubar getip”、“/bin/menubar/getuser”中 运行 它。我想根据以下工作代码创建一个函数。
import Cocoa
import Foundation
var task:NSTask = NSTask()
var pipe:NSPipe = NSPipe()
task.launchPath = "/bin/menubar"
task.arguments = ["getip"]
task.standardOutput = pipe
task.launch()
var handle = pipe.fileHandleForReading
var data = handle.readDataToEndOfFile()
var result_s = NSString(data: data, encoding: NSUTF8StringEncoding)
print(result_s)
我想把它转换成一个函数。
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 = NSString(data: data, encoding: NSUTF8StringEncoding)
return result_s
}
commmand getip
试试这个:
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"))
我是个彻头彻尾的 swift 菜鸟。在 xcode 中使用此代码我得到了我需要的结果。我创建了一个带有多个参数的命令行二进制文件 "menubar"。我通常在终端“/bin/menubar getip”、“/bin/menubar/getuser”中 运行 它。我想根据以下工作代码创建一个函数。
import Cocoa
import Foundation
var task:NSTask = NSTask()
var pipe:NSPipe = NSPipe()
task.launchPath = "/bin/menubar"
task.arguments = ["getip"]
task.standardOutput = pipe
task.launch()
var handle = pipe.fileHandleForReading
var data = handle.readDataToEndOfFile()
var result_s = NSString(data: data, encoding: NSUTF8StringEncoding)
print(result_s)
我想把它转换成一个函数。
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 = NSString(data: data, encoding: NSUTF8StringEncoding)
return result_s
}
commmand getip
试试这个:
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"))