2021年3月10日星期三

Swift getting Nil when attempting to get value from key that exists

Im not sure why i am getting this nil error on optional when attempting to print. Can somebody provide any input? It doesn't make sense that I am able to output the value from the aplicationContext, however when I attempt to get value from applicationContext["hearRate"] i get nil.

    func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {          print("didReceiveApplicationContext\n\t\(applicationContext)")          let hrValue = applicationContext["heartRate"] as? String          print(applicationContext["hearRate"])          print(hrValue)          heartRate.text = hrValue      }  
console - output  2021-03-10 18:41:04.623716-0700 Trainer+[1824:759737] Metal API Validation Enabled  session active state  didReceiveApplicationContext      ["heartRate": 00BPM]  nil  Optional("00BPM")  Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file Trainer_/ViewController.swift, line 57  2021-03-10 18:41:04.999001-0700 Trainer+[1824:759754] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file Trainer_/ViewController.swift, line 57  
https://stackoverflow.com/questions/66575388/swift-getting-nil-when-attempting-to-get-value-from-key-that-exists March 11, 2021 at 09:50AM

没有评论:

发表评论