为什么类型的主窗口是双选的? [英] Why is main window of type double optional?

查看:123
本文介绍了为什么类型的主窗口是双选的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

访问 UIapplication的主窗口时,它会以 UIWindow?

When accessing UIapplication's main window it is returned as a UIWindow??

let view = UIApplication.sharedApplication().delegate?.window // view:UIWindow??

为什么它作为双重可选项返回,它是什么意思,如果放入 if if 我应该在之后添加一个吗?

Why is it returning as a double optional and what does it mean and if put into a if let should I add one ! after it?

if let view = UIApplication.sharedApplication().delegate?.window!

我的第一个是替换委托之后的,但这不是解决方案。

My first though was to replace ? with a ! after delegate but that was not the solution.

推荐答案

@ matt有细节,但有一个(有点可怕,有点令人敬畏)的解决方法。 (请参阅下面的编辑)

@matt has the details, but there is a (somewhat horrible, somewhat awesome) workaround. (See edit below, though)

let window = app.delegate?.window??.`self`()

我将理解这行代码作为读者的练习。

I will leave the understanding of this line of code as an exercise for the reader.

好吧,我撒谎,让我们分解。

OK, I lie, let's break it down.

app.delegate?.window

好的,到目前为止一切顺利。此时我们有 UIWindow ?? 让我们头痛(我相信是Swift中的错误在Swift和Cocoa之间断开连接) 。我们想要崩溃两次。我们可以通过可选链接(?。)来实现这一点,但是这会解开并重新打包,所以我们回到了我们开始的地方。你可以使用 ??。来双重可选链,这很奇怪但是很有效。

OK, so far so good. At this point we have the UIWindow?? that is giving us a headache (and I believe is a bug in Swift disconnect between Swift and Cocoa). We want to collapse it twice. We can do that with optional chaining (?.), but that unwraps and rewraps, so we're back where we started from. You can double-optional-chain, though, with ??. which is bizarre, but works.

那是很好,但 ?? 不是合法的后缀运算符。你必须实际链接到某事。好吧,我们想要回归自己(即身份)。 NSObject 协议为我们提供了一种身份方法: self

That's great, but ?? isn't a legal suffix operator. You have to actually chain to something. Well, we want to chain back to itself (i.e. "identity"). The NSObject protocol gives us an identity method: self.

self NSObject 上的一个方法,但它也是Swift中的保留字,所以它的语法是`self`()

self is a method on NSObject, but it's also a reserved word in Swift, so the syntax for it is `self`()

所以我们疯狂了。照你的意愿去做。

And so we get our madness above. Do with it as you will.

请注意,由于 ??。有效,因此从技术上讲,您不需要这样做。您可以接受视图 UIWindow ?? 并使用 ??。就像 view ??。frame 。它有点吵,但可能不会为它应该需要的几个地方造成任何实际问题。

Note that since ??. works, you don't technically need this. You can just accept that view is UIWindow?? and use ??. on it like view??.frame. It's a little noisy, but probably doesn't create any real problems for the few places it should be needed.

(*)我曾经认为这是一个bug在Swift中,但它不能通过可选链接直接修复。问题是没有可选的链接超过窗口。所以我不确定在哪里找到合适的地方。 Swift可以允许后缀 - 表示变平而不需要链接,但这感觉很奇怪。我想正确的运算符是interrobang 委托?.window‽:D我确信这不会引起任何混淆。

(*) I used to think of this as a bug in Swift, but it's not fixable directly by optional chaining. The problem is that there is no optional chaining past window. So I'm not sure where the right place to fix it is. Swift could allow a postfix-? to mean "flatten" without requiring chaining, but that feels odd. I guess the right operator would be interrobang delegate?.window‽ :D I'm sure that wouldn't cause any confusion.

编辑:

Joseph Lord指出了更好的解决方案(这与我一直用来避免琐碎的if-let,但之前没想过这种方法的技术非常类似):

Joseph Lord pointed out the better solution (which is very similar to techniques I've been using to avoid trivial if-let, but hadn't thought of this way before):

let window = app.delegate?.window ?? nil // UIWindow?

我同意他这是正确的答案。

I agree with him that this is the right answer.

这篇关于为什么类型的主窗口是双选的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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