xcode - Invoking a custom Timer Class -


i've cobbled timer class use nstimer viewcontrollers, code below

import foundation  class timer {    var timer = nstimer()    var handler: (int) -> ()     let duration: int    var elapsedtime: int = 0     init(duration: int, handler: (int) -> ()) {       self.duration = duration       self.handler = handler    }     func start() {       self.timer = nstimer.scheduledtimerwithtimeinterval(30,           target: self,           selector: selector("chkprogress"),           userinfo: nil,           repeats: true)    }     func stop() {       timer.invalidate()    }     @objc func chkprogress() {       self.elapsedtime++        self.handler(elapsedtime)        if self.elapsedtime == self.duration {           self.stop()       }    }     deinit {       self.timer.invalidate()    } } 

then in each viewcontroller can check time progress , display message accordingly. problem when try invoke it

i've tried

var timer = timer() var timer = timer(30) var timer = timer(30,30) 

the resulting message same telling me cannot invoke timer type (int,int) how duration() in init set out

what doing wrong?

the parameters not take int, int. take int, , closure.

https://developer.apple.com/library/ios/documentation/swift/conceptual/swift_programming_language/closures.html

you need fix timer code well;

class timer {     var timer = nstimer()     var handler: (x:int) -> ()      let duration: int     var elapsedtime: int = 0      init(duration: int, handler: (int) -> ()) {         self.duration = duration         self.handler = handler     }      func start() {         self.timer = nstimer.scheduledtimerwithtimeinterval(30,             target: self,             selector: selector("chkprogress"),             userinfo: nil,             repeats: true)     }      func stop() {         timer.invalidate()     }      @objc func chkprogress() {         self.elapsedtime++          self.handler(x: elapsedtime)          if self.elapsedtime == self.duration {             self.stop()         }     }      deinit {         self.timer.invalidate()     } } 

//implementation of timer

var t = timer(duration: 30, handler:{ (x:int) -> void in             //do soemthing x;         }); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -