Repeat every three seconds using Time.
I used it to create an app that functions like a clock.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
var timer: Timer! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
//3秒ごとに繰り返す、repeat every 3 seconds | |
timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.update), userInfo: nil, repeats: true) | |
timer.fire() | |
} | |
override func viewWillDisappear(_ animated: Bool) { | |
super.viewWillDisappear(true) | |
timer.invalidate() | |
} | |
@objc func update(tm: Timer) { | |
//この関数を繰り返す、repeat this function | |
print("Hello SwiSwiSwift") | |
} | |
} |