-1

I am trying to call a function with optional CompletionHandler parameter from a timer. Below is my code snippet:

typealias CompletionHandler = () -> Void

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.check(_:)), userInfo: nil, repeats: false)
    }

    func check(completion: CompletionHandler?) {
        print("userinfo1")
    }

}

When the timer is triggered after 1.0 second, my app is dead with EXC_BAD_ACCESS and the pointer points to class AppDelegate: UIResponder, UIApplicationDelegate in AppDelegate.

Can you show me how I can call check function with such parameter? (If completion is just an Int, the timer did work!)

Thanks

quanguyen
  • 1,443
  • 3
  • 17
  • 29

1 Answers1

-1

You can create a timer like this:

NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(connectionTimeoutExpired(_:)), userInfo: nil, repeats: false)

func connectionTimeoutExpired(sender: NSTimer) {
        print("Connection Timeout Expired")
        connectionTimer?.invalidate()
        connectionTimer = nil
    }
Diogo Antunes
  • 2,241
  • 1
  • 22
  • 37
  • Reason I want to call `check` (not `check1` with `sender` as `NSTimer`) is I already had `check` and I want to reuse it. – quanguyen Jun 22 '16 at 11:12