Given the function "printTime" that prints 3 statements (H/M/S) what is the exact syntax for calling this function using NSTimer interrupts once per second?
Asked
Active
Viewed 233 times
1 Answers
2
Try this:
import Foundation
import XCPlayground
class Wrapper: NSObject {
static let formatter: NSDateFormatter = {
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm:ss"
return formatter
}()
class func printTime() {
print(formatter.stringFromDate(NSDate()))
}
}
let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: Wrapper.self, selector: #selector(Wrapper.printTime), userInfo: nil, repeats: true)
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
You need to wrap the printTime
function in an object to send to NSTimer
. The loop will end when you change the playground's code.

Code Different
- 90,614
- 16
- 144
- 163