-3

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?

Levaithai
  • 1
  • 1
  • We need for info. Please post code for printTime. –  Mar 29 '16 at 00:45
  • Possible duplicate of [Using NSTimer in swift playground](http://stackoverflow.com/questions/29232334/using-nstimer-in-swift-playground) – nhgrif Mar 29 '16 at 00:55

1 Answers1

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