[英]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 value
在lName.setHidden()
fatal error: unexpectedly found nil while unwrapping an Optional value
时fatal 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.