简体   繁体   English

在实际设备上的Glance中访问IBOutlet时,WatchKit“在解开可选值时意外发现nil”

[英]WatchKit “unexpectedly found nil while unwrapping an Optional value” when accessing IBOutlet in Glance on actual device

I have a Glance and some WKInterfaceLabel s. 我有一个Glance和一些WKInterfaceLabel I use setHidden() on them in override func willActivate() depending on some conditions. 根据某些条件,我在它们上面使用setHidden() override func willActivate()

class GlanceController: WKInterfaceController {
  @IBOutlet weak var lName: WKInterfaceLabel!
  ...

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
    if(conditions) {
        lName.setHidden(false)
    } else {
        lName.setHidden(true)
    }
  }
}

This works in simulator but on actual watch, I get fatal error: unexpectedly found nil while unwrapping an Optional value at lName.setHidden() . 这在模拟器中有效,但在实际观察中,我会遇到fatal error: unexpectedly found nil while unwrapping an Optional valuelName.setHidden() fatal error: unexpectedly found nil while unwrapping an Optional valuefatal error: unexpectedly found nil while unwrapping an Optional value

Anyone saw this before? 有人看过吗?

As your "lName" is declared as explicitly unwrapped optional, it safer to access the variable following way - 由于您的“ lName”被声明为显式展开的可选字段,因此按照以下方式更安全地访问变量-

   if let validLName = lName {
     if(conditions) {
        validLName.setHidden(false)
     } else {
        validLName.setHidden(true)
     }
   }
}

It appears that you are not allowed to use .setHidden() in Glance , at least for the current version of WatchKit . 似乎至少在当前版本的WatchKit ,不允许您在Glance使用.setHidden()

I redesign my UI completely to use a single label and it works. 我完全重新设计了UI以使用单个标签,并且可以正常工作。 Obviously it does not look as nice as I intend. 显然,它看起来并不像我预期的那样好。

I understand the restriction but really hope there is more documentation to save the trouble. 我了解此限制,但我确实希望有更多文档来避免麻烦。 Just like I only found out I can't scroll in Glance after spending time designing the UI. 就像我只发现花了很多时间在设计UI之后就无法滚动Glance

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IBOutlet:意外找到nil,同时隐式展开Optional值 - IBOutlet: Unexpectedly found nil while implicitly unwrapping an Optional value 解包可选的 IBOutlet 时出现错误(线程 1:致命错误:解包可选值时意外发现 nil) - I have an error when unwrapping an optional IBOutlet (Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value) 展开Optional值时意外发现nil - unexpectedly found nil while unwrapping an Optional value 展开可选的value()时意外找到nil - Unexpectedly found nil while unwrapping an Optional value() 致命错误:访问URL时,在展开可选值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value when accessing URL 使用不存在的键访问字典时,在展开可选值时意外发现nil - unexpectedly found nil while unwrapping an Optional value when accessing dictionary with non-existent key 添加UINavigation时,“在展开可选值时意外发现nil” - “Unexpectedly found nil while unwrapping an Optional value” When UINavigation is added 加载tableView时解开可选值时意外发现nil - Unexpectedly found nil while unwrapping an optional value when loading tableView WatchKit WKInterfaceMovie致命错误:展开一个可选值时意外发现nil - WatchKit WKInterfaceMovie fatal error: unexpectedly found nil while unwrapping an Optional value 展开Optional值时意外发现nil - Unexpectedly found nil when unwrapping an Optional value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM