First of all, your onTimer method must be declared as @objc , or NSTimer cannot find it.
As for your question, this is because you did not start a run loop.
For this, CFRunLoopRun() is the simplest solution I think.
import Foundation class MyClass { func startTimer() { NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "onTimer:", userInfo: nil, repeats: true) } @objc func onTimer(timer:NSTimer!) { println("Timer here") } } var anInstance = MyClass() anInstance.startTimer() CFRunLoopRun()
For completeness, like @MartinR seen in a comment, you can also use XCPSetExecutionShouldContinueIndefinitely()
import Foundation import XCPlayground XCPSetExecutionShouldContinueIndefinitely() class MyClass { func startTimer() { NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "onTimer:", userInfo: nil, repeats: true) } @objc func onTimer(timer:NSTimer!) { println("Timer here") } } var anInstance = MyClass() anInstance.startTimer()
In this case, the playground only starts the seconds indicated on the timeline:

source share