ios - Selector is not fired from scheduledTimerWithTimeInterval -
i checked existing posts on topic , googled it, not able identify mistake or make work me. have function iterativedeepening() inside class chessplayer. after 15 seconds want stop further iterations within function. in code below, function "flagsetter" never invoked. if use nstimer.fire() function invoked , not after 15 seconds. tried placing flagsetter function before or after iterativedeepening(). either case not work. have done incorrectly?
class chessplayer { var timeoutflag = false //code func iterativedeepening() { ***//variables , constants*** let timer = nstimer.scheduledtimerwithtimeinterval(15.0, target: self, selector: #selector(self.flagsetter), userinfo: nil, repeats: false) ***while mindepth <= maxdepth { // loop iteration code if timeoutflag { break out of loop } }*** } @objc func flagsetter(timer: nstimer) { print("flag changed true") self.timeoutflag = true timer.invalidate() } }
the requirement:
- computerthinking() fired gamescene human move's action completion handler.
- gamescene.computerthinking() invokes chessplayer.iterativedeepening()
- iterativedeepening runs while loop incrementing "depth". each "depth" optimal move @ depth evaluated. higher depth, more detailed evaluation.
- after 15.0 seconds want break out of while loop depth , optimal move available @ point of time.
i lover of objective-c , never used swift yet in projects. googling nstimer swift, found out following steps implement nstimer correctly.
we need define our nstimer. first variable going need variable called timer of type nstimer. like:
var timer = nstimer()
start nstimer timer:
timer = nstimer.scheduledtimerwithtimeinterval(15.0, target:self, selector:#selector(chessplayer.flagsetter(_:)), userinfo: nil, repeats: false)
and method flagsetter should defined this:
func flagsetter(timer: nstimer) { print("flag changed true") self.timeoutflag = true timer.invalidate() }
this surely work i've made first app, question, made in swift. check how put selector. you're right warnings way.
if need more information selectors, check thread: @selector() in swift?
Comments
Post a Comment