尝试从存在的键中获取价值时迅速获得Nil [英] Swift getting Nil when attempting to get value from key that exists

查看:47
本文介绍了尝试从存在的键中获取价值时迅速获得Nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定在尝试打印时为什么在可选中出现此nil错误.有人可以提供任何输入吗?我无法从 aplicationContext 输出值是没有意义的,但是,当我尝试从 applicationContext ["hearRate"] 获取值时,我得到的值是nil

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

更新-忽略拼写错误,发现IBOutlet存在问题

Update - Ignore typo, found issue with IBOutlet

    func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
        print("didReceiveApplicationContext\n\t\(applicationContext)")
        let hrValue = applicationContext["heartRate"] as? String
        print(applicationContext["heartRate"])
        print(hrValue)
        //heartRate.text = hrValue
        heartRate.text = "test"
    }

2021-03-11 12:45:05.482464-0700 Trainer+[1873:817768] Metal API Validation Enabled
session active state
didReceiveApplicationContext
    ["heartRate": 91BPM]
Optional(91BPM)
Optional("91BPM")
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file Trainer_/ViewController.swift, line 58
2021-03-11 12:45:05.887523-0700 Trainer+[1873:817966] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file Trainer_/ViewController.swift, line 58

来自watchconnectivity的func会话(didReceiveApplicationContext)正在按预期工作.我认为我的问题来自将heartRate标签移动到视图容器".我在移动标签的同时添加了表连接性,并认为它可以正常工作.

the func session(didReceiveApplicationContext), from watchconnectivity is working as intended. I believe that my problem comes from me moving the heartRate label to a 'view container'. I added the watchconnectivity at the same time that I moved the label and assumed that it would work.

这可能需要一个不同的问题,不确定,我如何才能沿同时可见的多个视图控制器传递数据.由于我是在ViewController上创建会话的,因此我还不知道如何在不将ViewController的完整自我"引用传递到DetailViewController的情况下将数据传递给其他视图控制器.来自Java和PHP,我觉得这不是很好的编码习惯.

This may require a different question, not sure, how can i pass data along multiple view controllers that are visible at the same time. Since I am creating the session on the ViewController, I don't yet know how to pass the data to other view controllers without passing a full 'self' reference of the ViewController, to the DetailViewController. Coming from Java and PHP, i feel like this is not good coding practice.

我正在考虑扩展/代理和原型.我对它们有一定程度的了解,但是我目前正在开发一个应用程序,我的主要目标是拥有一个可行的原型.我将返回并重构代码修复并修复所有重大漏洞.如果有经验的机构可以为我提供任何可靠的资源.厌倦了点击谷歌的下一页,因为我找不到我正在寻找的答案.

I am thinking about extensions/delegates, and prototypes. I understand them to a slight degree, but I am currently working on an app and my main goal is to have a working prototype. I'll come back and refactor the code fix and fix any gaping vulnerabilities. If any body with experience can provide me with any reliable resources. Tired of hitting the next page of google as i cant find the answers that I am looking for lol.

推荐答案

如果您复制并粘贴了代码,我认为这只是拼写错误.您可以尝试更改以下行:

If you have copied and pasted your code, I think it's just misspelling. You may try changing this line:

print(applicationContext["hearRate"])

收件人:

print(applicationContext["heartRate"])

这篇关于尝试从存在的键中获取价值时迅速获得Nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆