ImplicitlyUnwrappedOptional in init vs later [英] ImplicitlyUnwrappedOptional in init vs later

查看:22
本文介绍了ImplicitlyUnwrappedOptional in init vs later的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解为什么我在执行 params["bar"] = str 时没有得到 ImplicitlyUnwrappedOptional 但我在声明 时得到了它params 具有相同的强制解包变量.

请看下面的游乐场:

导入 UIKitvar str:字符串!str = "你好"var 参数:[字符串:任意] = [foo":str]参数["bar"] = str打印(参数)//["bar": "Hello", "foo": Swift.ImplicitlyUnwrappedOptional<Swift.String>.some("Hello")]

解决方案

在 Swift 4.1 中,当您这样做时:

var str: 字符串!str = "你好"var 参数:[字符串:任意] = [foo":str]

ImplicitlyUnwrappedOptional (IUO) 值被强制转换为 Any,这就是它在字典中显示为 IUO 的原因.它不会被强制解包,因为编译器只会在上下文要求其解包类型时强制解包 IUO,而 Any 不是这种情况.

然而,你最终得到一个 ImplicitlyUnwrappedOptional value 的事实是遗留行为.随着 Swift 4.2 中 IOU 类型的删除,您将在字典中获得一个 Optional 值,该值将打印为 Optional("Hello").>

在此问答中对上述行为进行了更多讨论:

当你这样做时:

params["bar"] = str

您正在使用 Dictionarysubscript(key: Key) ->值?,它接受一个 Optional 值——如果 nil 被传递,则执行删除,否则插入解包的值.

  • 在 Swift 4.1 中,IUO 值 str 将隐式转换为 Optional,然后可以传递给下标.
  • 在 Swift 4.2 中,IUO 类型已经被移除,所以 str 已经 is 一个 Optional,可以直接传递给下标没有任何中间转换.

在这两种情况下,str 的解包值都被插入到字典中,这就是您将其视为解包的原因.如果 strnil,则不会插入键 "bar" 的值.

I would like to understand why I don't get ImplicitlyUnwrappedOptional when I do params["bar"] = str but I get it when I declare params with the same force unwrapped variable.

See the playground below:

import UIKit

var str: String!

str = "Hello"

var params: [String: Any] = [
    "foo": str
]

params["bar"] = str

print(params)

// ["bar": "Hello", "foo": Swift.ImplicitlyUnwrappedOptional<Swift.String>.some("Hello")]

解决方案

In Swift 4.1, when you do:

var str: String!

str = "Hello"

var params: [String: Any] = [
    "foo": str
]

The ImplicitlyUnwrappedOptional (IUO) value is coerced to Any, which is why it appears as an IUO inside your dictionary. It won't be force unwrapped, because the compiler will only force unwrap an IUO when the context demands its unwrapped type, which isn't the case with Any.

However the fact that you end up with an ImplicitlyUnwrappedOptional value is legacy behaviour. With the removal of the IUO type in Swift 4.2, you'll get an Optional value inside your dictionary instead, which will print as Optional("Hello").

There's more discussion of the above behaviour in this Q&A:

When you do:

params["bar"] = str

You're using Dictionary's subscript(key: Key) -> Value?, which takes an Optional value – performing a removal if nil is passed, otherwise doing an insertion of the unwrapped value.

  • In Swift 4.1, the IUO value str will be implicitly converted to an Optional which can then be passed to the subscript.
  • In Swift 4.2, the IUO type has been removed, so str already is an Optional, which can be passed straight to the subscript without any intermediate conversions.

In both cases, str's unwrapped value is inserted into the dictionary, which is why you see it as being unwrapped. If str had been nil, no value for the key "bar" would have been inserted.

这篇关于ImplicitlyUnwrappedOptional in init vs later的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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