简体   繁体   中英

ios: Timer working in simulator but not on device

I am currently using a time on a Swift application. In previous app I had no problems but for this one I had a particular behavior: the timer works fine on the simulator but is never fired on the real device (10.0.2).

NSTimer.scheduledTimerWithTimeInterval(5.0, target:self, selector: #selector(AppDelegate.testTimer), userInfo: nil, repeats: true);

Here is the function called

func testTimer(){
  print("Timer called")
}

I don't see anything wrong with this... I've tried to create this timer at various place with the same results....

EDIT: I am currently in Swift 2.3

EDIT this is only the case in Swift 3

I couldn't get your code to work on the simulator or the device, but it works on both like this

let appDelegate = UIApplication.shared.delegate as! AppDelegate
Timer.scheduledTimer(timeInterval: 1.0, target:appDelegate, selector: #selector(appDelegate.testTimer), userInfo: nil, repeats: true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM