我正在尝试制作一个日志记录系统,它从"log.txt“文件中读取并显示给我的ViewController中的一个NSTextView。这是我所拥有的
while(server.listen()) {
NSThread.sleepForTimeInterval(1)
self.consoleOutput.string! = log.read()
}log.read()输出一个"log.txt“文件。
self.consoleOutput.string!更新NSTextView
它马上就会结冰,有什么办法可以解决吗?这是我第一次创建日志系统。
发布于 2015-02-17 08:14:18
NSThread.sleepForTimeInterval(x)冻结了你的应用程序,所以这不是你想要的。您希望每秒钟执行一次代码,因此其中一种方法是使用NSTimer类。下面是一些您可以使用的示例:
var timer: NSTimer?
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerFired:", userInfo: nil, repeats: true)
timer?.fire()
}
func timerFired(timer: NSTimer) {
self.consoleOutput.string! = log.read()
}以停止您调用timer?.invalidate()的计时器。
https://stackoverflow.com/questions/28556531
复制相似问题