gpt4 book ai didi

swift - DispatchQueue.main.asyncAfter(截止日期: ) not working in command line app

转载 作者:行者123 更新时间:2023-12-03 17:46:41 26 4
gpt4 key购买 nike

我正在尝试制作一个程序,每秒从 txt 文件粘贴一个句子。它可以在没有 DispatchQueue.main.asyncAfter(deadline: ) 的情况下工作。有了它,它什么也不打印,并立即以退出代码 0 结束。程序如下:

import Cocoa

let file = "slowreadText.txt"
let waitTime: TimeInterval = 1

if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {

let fileURL = dir.appendingPathComponent(file)

do {
let text = try String(contentsOf: fileURL, encoding: .utf8)
let sentences = text.components(separatedBy: ".")

for i in 0..<sentences.count {
let sentence = sentences[i]
DispatchQueue.main.asyncAfter(deadline: .now() + (waitTime * Double(i))) {
print(sentence)
}
}

}catch {
print(error)
}
}

最佳答案

您需要在脚本末尾调用 RunLoop.main.run() 以防止其终止。

关于swift - DispatchQueue.main.asyncAfter(截止日期: ) not working in command line app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54900745/

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