设置窗口背景颜色时无法解开 Optional.None [英] Can't unwrap Optional.None when setting window background color

查看:21
本文介绍了设置窗口背景颜色时无法解开 Optional.None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我在这里遗漏了一些非常简单的东西...

I think I'm missing something very simple here...

override func viewDidLoad() {
    self.view.window.backgroundColor = NSColor.grayColor();                            
}

返回

fatal error: Can't unwrap Optional.None

这不是一个非常具有描述性的错误.有人能告诉我我错过了什么吗?

which isn't a very descriptive error. Can someone tell me what I'm missing?

推荐答案

self.view.window.backgroundColor 链中的东西(可能是 self.view.window)为零.许多与视图和视图控制器相关的属性被实现为隐式解包,这意味着它们是可选的,为了方便起见,您可以将它们视为非可选变量.不幸的是,如果您尝试在它为零时访问一个,则会出现您所看到的运行时错误.

Something in the self.view.window.backgroundColor chain (probably self.view.window) is nil. Many view- and view controller-related properties are implemented as implicitly unwrapped, which means that they are Optionals that you can treat like non-Optional variables for convenience's sake. Unfortunately, if you try to access one when it's nil you get the runtime error you're seeing.

你可以设置视图层的背景颜色吗?view.window 在视图尚未添加到窗口时为零,但无论如何 view.layer 都应该在那里.

Can you set the background color of the view's layer instead? view.window is nil when a view hasn't been added to a window, but view.layer should be there regardless.

这篇关于设置窗口背景颜色时无法解开 Optional.None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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