使用Swift创建日志系统

时间:2015-02-17 07:11:18

标签: ios macos swift logging

我试图建立一个从" log.txt"文件并将其显示在ViewController中的NSTextView中。这就是我所拥有的,

while(server.listen()) {
    NSThread.sleepForTimeInterval(1)
    self.consoleOutput.string! = log.read()
}

log.read()输出" log.txt"文件。

self.consoleOutput.string!更新NSTextView

它会立即冻结,任何有关如何处理此问题的想法?这是我第一次创建日志系统。

1 个答案:

答案 0 :(得分:0)

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()