2
Swift 앱에서 타이머를 사용하고 있습니다. 타이머를 생성하고 Runloop에 삽입 한 후에는 Timer에 대한 참조를 유지하지 않는 것이 좋습니다. 나는 그것을 무효로하고 싶다. 참조를 유지하지 않고이를 수행 할 수있는 방법이 있습니까?RunLoop에 배치 된 타이머를 무효화하는 방법
Swift 앱에서 타이머를 사용하고 있습니다. 타이머를 생성하고 Runloop에 삽입 한 후에는 Timer에 대한 참조를 유지하지 않는 것이 좋습니다. 나는 그것을 무효로하고 싶다. 참조를 유지하지 않고이를 수행 할 수있는 방법이 있습니까?RunLoop에 배치 된 타이머를 무효화하는 방법
타이머 선택기는 Timer
개체에 대한 참조를 유지할 수 있습니다. 이것을 시도하십시오 :
class ViewController: UIViewController {
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
let _ = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.timerFired(timer:)), userInfo: nil, repeats: true)
}
// Run the timers for 3 times then invalidate it
func timerFired(timer: Timer) {
if count < 3 {
count += 1
print(count)
} else {
timer.invalidate()
print("Timer invalidated")
}
}
}
Ahh, brilliant! 정확히 내가 염두에 두었던 것. 격려. – dugla