mainrunloop

Swift cannot output when using NSTimer

北城以北 提交于 2019-12-11 00:43:30
问题 I tried output to STDOUT every second by using NSTimer . I wrote the following code and saved it as sample.swift. #!/usr/bin/swift import AppKit class Foo : NSObject { override init() { super.init() NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "bar", userInfo: nil, repeats: true) } func bar() { print("buz") } } let h = Foo() // Output successful // while true { // print("Foo!") // } NSRunLoop.mainRunLoop().run() Then, I executed the following command and can see buz .