Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 Timer in a Class not firing

Tags:

swift

timer

I have a class:

class GameManager {...

and within it I have this func:

func startGame() {

        msgTimer = Timer(timeInterval: 0.5, target: self, selector: #selector(typeMessage(_:)), userInfo: nil, repeats: true)

}

and the selector it calls:

@objc func typeMessage(_ sender:Timer) {

        if textCount > strInitText.characters.count {
            let strThisChar = strInitText[strInitText.index(strInitText.startIndex, offsetBy: textCount)]
            strDisplayText = strDisplayText + String(strThisChar)
            print(strDisplayText)
        }

    }

But the selector never gets called.

enter image description here

like image 427
PruitIgoe Avatar asked Nov 28 '22 22:11

PruitIgoe


1 Answers

Change

msgTimer = Timer(timeInterval: 0.5, target: self, selector: #selector(typeMessage(_:)), userInfo: nil, repeats: true)

to

msgTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(typeMessage(_:)), userInfo: nil, repeats: true)
like image 168
CodeNinja Avatar answered Dec 05 '22 07:12

CodeNinja