什么是“展开的值"?在斯威夫特? [英] What is an "unwrapped value" in Swift?

查看:25
本文介绍了什么是“展开的值"?在斯威夫特?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照

解决方案

首先,你要了解什么是 Optional 类型.可选类型基本上意味着变量可以是 nil.

例子:

var canBeNil : Int?= 4canBeNil = 无

问号表示canBeNil可以是nil.

这行不通:

var cantBeNil : Int = 4cantBeNil = nil//不能这样做

如果变量是可选的,要从变量中获取值,您必须解包.这只是意味着在末尾加上一个感叹号.

var canBeNil : Int?= 4println(canBeNil!)

您的代码应如下所示:

让 optionalSquare: Square?= Square(sideLength: 2.5, name: "可选正方形")让 sideLength = optionalSquare!.sideLength

附注:

您还可以使用感叹号而不是问号来声明可自动解包的选项.

例子:

var canBeNil : Int!= 4print(canBeNil)//不需要展开

因此,修复代码的另一种方法是:

let optionalSquare: Square!= Square(sideLength: 2.5, name: "可选正方形")让 sideLength = optionalSquare.sideLength

您看到的差异正是可选值被包装这一事实的症状.上面还有一层.unwrapped 版本只显示直线对象,因为它是展开的.

快速的游乐场比较:

​​

在第一种和第二种情况下,对象不会自动展开,因此您会看到两个层"({{...}}),而在第三种情况下,您会看到只有一层({...}),因为对象正在自动展开.

第一种情况和后两种情况的区别在于,如果 optionalSquare 设置为 nil,后两种情况会给你一个运行时错误.使用第一种情况下的语法,您可以执行以下操作:

如果让 sideLength = optionalSquare?.sideLength {println("sideLength 不为零")} 别的 {println("边长为零")}

I'm learning Swift for iOS 8 / OSX 10.10 by following this tutorial, and the term "unwrapped value" is used several times, as in this paragraph (under Objects and Class):

When working with optional values, you can write ? before operations like methods, properties, and subscripting. If the value before the ? is nil, everything after the ? is ignored and the value of the whole expression is nil. Otherwise, the optional value is unwrapped, and everything after the ? acts on the unwrapped value. In both cases, the value of the whole expression is an optional value.

let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square") 
let sideLength = optionalSquare?.sideLength

I don't get it, and searched on the web without luck.

What does this means?


Edit

From Cezary's answer, there's a slight difference between the output of the original code and the final solution (tested on playground) :

Original code

Cezary's solution

The superclass' properties are shown in the output in the second case, while there's an empty object in the first case.

Isn't the result supposed to be identical in both case?

Related Q&A : What is an optional value in Swift?

解决方案

First, you have to understand what an Optional type is. An optional type basically means that the variable can be nil.

Example:

var canBeNil : Int? = 4
canBeNil = nil

The question mark indicates the fact that canBeNil can be nil.

This would not work:

var cantBeNil : Int = 4
cantBeNil = nil // can't do this

To get the value from your variable if it is optional, you have to unwrap it. This just means putting an exclamation point at the end.

var canBeNil : Int? = 4
println(canBeNil!)

Your code should look like this:

let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square") 
let sideLength = optionalSquare!.sideLength

A sidenote:

You can also declare optionals to automatically unwrap by using an exclamation mark instead of a question mark.

Example:

var canBeNil : Int! = 4
print(canBeNil) // no unwrapping needed

So an alternative way to fix your code is:

let optionalSquare: Square! = Square(sideLength: 2.5, name: "optional square") 
let sideLength = optionalSquare.sideLength

EDIT:

The difference that you're seeing is exactly the symptom of the fact that the optional value is wrapped. There is another layer on top of it. The unwrapped version just shows the straight object because it is, well, unwrapped.

A quick playground comparison:

In the first and second cases, the object is not being automatically unwrapped, so you see two "layers" ({{...}}), whereas in the third case, you see only one layer ({...}) because the object is being automatically unwrapped.

The difference between the first case and the second two cases is that the second two cases will give you a runtime error if optionalSquare is set to nil. Using the syntax in the first case, you can do something like this:

if let sideLength = optionalSquare?.sideLength {
    println("sideLength is not nil")
} else {
    println("sidelength is nil")
}

这篇关于什么是“展开的值"?在斯威夫特?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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