gpt4 book ai didi

swift - 有没有办法获得 ping 的结果,显示在文本框中,但没有旋转的色轮?

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:02 25 4
gpt4 key购买 nike

由于 ping 的结果可能需要超过 4 秒(取决于跳数),我得到了旋转的色轮(沙滩球),这通常表示程序已锁定或无法运行。

应用会继续旋转色轮,并在文本框中显示结果。有没有办法摆脱“沙滩球”?

我通过将结果打印到每个变量来进行调试。在执行“let handle1 = pipe.fileHandleForReading”后,沙滩球出现了。我尝试了 Grand Central Dispatch (GCD),但仍然得到相同的结果。

// Built using X-Code Version 8.2.1 - Swift Version 3.0.2

// Text view for results (display)
@IBOutlet weak var Results: NSTextField!

// Start button to execute ping
@IBAction func startButton(_ sender: Any)
{
cmdPing()
}

// Global variables
var myArg = ""
var shellResults = ""

func cmdPing()
{
// Step 1
myArg = "ping -c 10 www.google.com"
shellResults = getPing(shellArgs: myArg)
Results.stringValue = shellResults

// Step 2
myArg = "ping -c 10 127.0.0.1"
shellResults = getPing(shellArgs: myArg)
Results.stringValue = shellResults

}


// function that executes a shell command and returns its value (myArg)
func getPing(shellArgs: String) -> String
{
let task:Process = Process()
let pipe:Pipe = Pipe()
task.launchPath = "/bin/sh"
task.arguments = ["-c", shellArgs]
task.standardOutput = pipe
task.launch()
let handle1 = pipe.fileHandleForReading
let data1 = handle1.readDataToEndOfFile()
let result1 = NSString(data: data1, encoding: String.Encoding.utf8.rawValue)
let trimmed1 = (result1! as NSString).trimmingCharacters(in: NSCharacterSet.whitespaces)
shellResults = trimmed1
task.waitUntilExit()
return shellResults
}

我确实在文本框中得到了结果。如果可以做到的话,最好是在没有“沙滩球”的情况下,在每次 ping 期间在文本框中显示每一行(跃点)。任何帮助将不胜感激。

最佳答案

这才是有效的。它将执行命令并返回 TextView 或 ScrollView 窗口中的每个跃点。这将摆脱旋转的“沙滩球”。添加了一个计数器或者你可以做一个循环来继续你的许多任务(命令 ping)

func getPing(shellArgs: String)
{

let task = Process()
task.launchPath = "/bin/sh"
task.arguments = ["-c", shellArgs]

let pipe = Pipe()
task.standardOutput = pipe
let outHandle = pipe.fileHandleForReading
outHandle.waitForDataInBackgroundAndNotify()

var obs1 : NSObjectProtocol!
obs1 = NotificationCenter.default.addObserver(forName: NSNotification.Name.NSFileHandleDataAvailable, object: outHandle, queue: nil)
{
notification -> Void in let data = outHandle.availableData

if data.count > 0
{
if let str = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
{
// place data (str) in Results (textview) window
self.Results.stringValue += "\(str)"
}
outHandle.waitForDataInBackgroundAndNotify()
}
else
{
print("EOF on stdout from process")
NotificationCenter.default.removeObserver(obs1)
}
}

var obs2 : NSObjectProtocol!
obs2 = NotificationCenter.default.addObserver(forName: Process.didTerminateNotification, object: task, queue: nil)
{
notification -> Void in print("terminated")
NotificationCenter.default.removeObserver(obs2)
if self.pingCount < 3
{
// add counter and continue for other commands (steps)
self.pingCount += 1
self.cmdPing()
}
else
{
// end task
task.terminate()
}
}
task.launch()
}

关于swift - 有没有办法获得 ping 的结果,显示在文本框中,但没有旋转的色轮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56675679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com