什么是“未包装的价值”。在斯威夫特? [英] What is an "unwrapped value" in Swift?

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

问题描述

我正在按照

解决方案

首先,您必须了解什么是可选的类型是。可选类型基本上意味着变量可以 nil



示例:

  var canBeNil:Int? = 4 
canBeNil = nil

问号表示 canBeNil 可以 nil



这不起作用:

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

要获取变量中的值(如果它是可选的),您必须 打开它 。这只是意味着在结尾处加上感叹号。

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

您的代码应如下所示:

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

旁注:



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



示例:

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

所以另一种修复方法你的代码是:

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



编辑:



您所看到的差异正是可选值包裹这一事实的症状。它上面还有另一层。 unwrapped 版本只显示直接对象,因为它完好无法解开。



快速游乐场比较:





在第一种和第二种情况下,对象不会自动解包,因此您会看到两个图层( {{...}} ),而在第三种情况下,您只看到一个图层( {...} ),因为该对象正在自动解包。



第一种情况和后两种情况之间的区别在于,如果 optionalSquare 设置为 nil 。使用第一种情况下的语法,你可以这样做:

 如果让sideLength = optionalSquare?.sideLength {
println(sideLength is not nil)
} else {
println(sidelength is nil)
}


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天全站免登陆