xcode 快速包装和展开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35005961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:27:56  来源:igfitidea点击:

Swift Wrap and Unwrap

xcodeswift

提问by user3314399

I'm a little confused about Swift Wrap and Unwrap! So lets say this is my code:

我对 Swift Wrap 和 Unwrap 有点困惑!所以让我们说这是我的代码:

var name:String? = "FirstName"
print(name)

Does the print function automatically unwrap the name which is optional? so I do not need to say print(name!)in order to unwrap the name? I guess what I am trying to understand is these two are equivalent for unwraping an optional variable?

打印功能是否会自动解开可选的名称?所以我不需要说 print(name!)为了解开这个名字吗?我想我想理解的是这两个对于解开可选变量是等效的吗?

print("name")is just like saying print ("name"!)

print("name")就像说 print ("name"!)

The other question I have is about nil. is saying var name:String? = "FirstName"equivalent to saying var name:String? = nil. So does assigning a nil value wraps a variable?

我的另一个问题是关于零。说var name:String? = "FirstName"相当于说var name:String? = nil。那么分配一个 nil 值会包装一个变量吗?

回答by Fred Faust

When something can be nil it can be two things, it can be Some (the value of the given type) or it can be nil.

当某物可以为 nil 时,它可以是两件事,它可以是 Some(给定类型的值),也可以是 nil。

declaring something like this:

声明如下:

var name: String?

Means that the name variable can be nil, if you assigned a value to it you need to unwrap it to use it.

意味着 name 变量可以是 nil,如果你给它赋值,你需要解开它才能使用它。

name = "FirstName"

Now the name variable has been defined, however you still need to ensure it's not nil in some cases, in other cases however (such as when the string doesn't need to be not nil) optional chaining is used.

现在已经定义了 name 变量,但是您仍然需要确保在某些情况下它不是 nil,但是在其他情况下(例如当字符串不需要非 nil 时)使用可选链接。

Optional chaining allows the continuous evaluation of nil or some throughout a statement as long as it's not required to be not nil. If that is the case then you will need to unwrap it:

可选链允许在整个语句中对 nil 或 some 进行连续评估,只要它不需要非 nil 即可。如果是这种情况,那么您需要打开它:

let someThingRequiresAString = NeedAStringInitializer(string: name!)

In the above statement if name is nil the program will crash, there are several approaches to dealing with things like this, here's a quick example:

在上面的语句中,如果 name 为 nil 程序将崩溃,有几种方法可以处理这样的事情,这里有一个简单的例子:

if name != nil {

let someThingRequiresAString = NeedAStringInitializers(string: name!)
}

Here you know you can do this b/c name has been evaluated to not be nil. You can also use a nil coalescing operator, or a guard statement. Here's a quick example of nil coalescence in Swift:

在这里您知道您可以执行此 b/c 名称已评估为不为零。您还可以使用 nil 合并运算符或保护语句。这是 Swift 中 nil 合并的快速示例:

let someThingRequiresAString = NeedAStringInit(string: name ?? "New Name")

The optional paradigm is quite powerful and expressive.

可选范式非常强大且富有表现力。