Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HeartRateRecevier {
- var delegate: HeartRateReceiverDelegate?
- var currentHeartRate: Int? {
- didSet {
- if let currentHeartRate = currentHeartRate {
- delegate?.heartRateUpdate(to: currentHeartRate)
- //print("The most recent heart rate received was (currentHeartRate)")
- } else {
- print("Looks like we can't pick up a heart rate")
- }
- }
- }
- func startHeartRateMonitoringExample(){
- for _ in 1...10 {
- let randomHeartRate = 60 + Int(arc4random_uniform(UInt32(15)))
- currentHeartRate = randomHeartRate
- Thread.sleep(forTimeInterval: 2)
- }
- }
- }
- protocol HeartRateReceiverDelegate {
- func heartRateUpdate(to bpm: Int)
- }
- class HeartRateViewController: UIViewController, HeartRateReceiverDelegate {
- @IBOutlet weak var heartRateLabel: UILabel!
- var heartRateReceiver = HeartRateRecevier()
- override func viewDidLoad() {
- super.viewDidLoad()
- heartRateReceiver.delegate = self
- }
- // Here we have the mandatory function of the protocol
- func heartRateUpdate(to bpm: Int) {
- heartRateLabel.text = "The heart rate is: (bpm)"
- }
- @IBAction func beginTestButton() {
- heartRateReceiver.startHeartRateMonitoringExample()
- }
- }
Add Comment
Please, Sign In to add comment