- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在通过关注 this tutorial 学习适用于 iOS 8/OSX 10.10 的 Swift ,并且术语“展开的值”被多次使用,如本段中所示(在对象和类下):
When working with optional values, you can write ? before operationslike methods, properties, and subscripting. If the value before the ?is nil, everything after the ? is ignored and the value of the wholeexpression is nil. Otherwise, the optional value is unwrapped, andeverything after the ? acts on the unwrapped value. In both cases, thevalue of the whole expression is an optional value.
let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square")
let sideLength = optionalSquare?.sideLength
我没明白,在网上搜索没有运气。
这是什么意思?
<小时/>从 Cezary 的回答来看,原始代码的输出和最终解决方案(在 Playground 上测试)之间存在细微差别:
在第二种情况下,父类(super class)的属性显示在输出中,而第一种情况下有一个空对象。
这两种情况的结果不应该是相同的吗?
最佳答案
首先,您必须了解什么是可选类型。可选类型基本上意味着变量可以nil
。
示例:
var canBeNil : Int? = 4
canBeNil = nil
问号表示 canBeNil
可以为 nil
。
这行不通:
var cantBeNil : Int = 4
cantBeNil = nil // can't do this
要从变量中获取值(如果该值是可选的),您必须解开它。这只是意味着在末尾添加一个感叹号。
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) // no unwrapping needed
因此修复代码的另一种方法是:
let optionalSquare: Square! = Square(sideLength: 2.5, name: "optional square")
let sideLength = optionalSquare.sideLength
您所看到的差异正是可选值被包装这一事实的症状。它上面还有另一层。 未包装版本仅显示直线对象,因为它是未包装的。
Playground 快速比较:
在第一种和第二种情况下,对象不会自动展开,因此您会看到两个“层”({{...}}
),而在第三种情况下,您会看到只有一层 ({...}
),因为对象会自动展开。
第一种情况和后两种情况之间的区别在于,如果将 opticalSquare
设置为 nil
,则后两种情况会给您带来运行时错误。使用第一种情况的语法,您可以执行以下操作:
if let sideLength = optionalSquare?.sideLength {
println("sideLength is not nil")
} else {
println("sidelength is nil")
}
关于ios - Swift 中的 "unwrapped value"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548151/
func initializePickerViewProperties() { let font = UIFont (name: "SanFranciscoDisplay-Regular",
我试图将预先附加的字符串作为 URL 请求传递,但我不断收到错误消息: fatal error :在展开可选值时意外发现 nil 此错误指向行:let searchTerm = "http://goo
我有一个像这样的html结构 旁边的两个 div 确实有类和子元素,但为了这个问题,您不需要知道它们是什么。在 DOM 准备就绪后,我将两个内部 div 包裹在一个新的 div 中,就
显然是我的语法错误。 我有大量的 div,其中少量的内容包含在 anchor 中。我要做的是移除 anchor 。 感谢您的帮助! http://jsfiddle.net/danielredwood/
关闭。这个问题是 opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 已关闭 2 年前。 奥 git _
我已经包装了阶段,我想使用“wrap”函数来打开它们。但是,在这种情况下,由于我不清楚的原因,我无法获得展开的相位。 包裹的阶段是 通过观察横截面,我可以看到相被包裹 我已将图像从 0 缩放到 2*p
用 jQuery 包装 div 后,展开它们然后尝试再次包装它们似乎会丢失 float 属性(我的猜测),我不知道如何解决这个问题。 当第一次按下 JSFiddle 中的换行按钮时,它会像我期望的那样
我将容器 .vorteile 通过 jQuery 包装在 .vorteile_outer 中。当我想在 .vorteile 上使用 .unwrap() 删除 .vorteile_outer 时,.vo
这个问题已经有答案了: jQuery : remove element except inside element (4 个回答) 已关闭 9 年前。 jquery中有一个很棒的方法叫做wrap()它
这个问题已经有答案了: jQuery : remove element except inside element (4 个回答) 已关闭 9 年前。 我有一段 HTML 代码: table 我如何从
当用户提交或聚焦无效输入时,我有一个包含在错误 div 中的文本字段。如果用户纠正了错误,我想删除包装器,但是,unwrap() 方法会导致字段失去焦点。 $(special_input).live(
我正在寻找一种方法来“解开”已发布到 MVC 核心服务的 JSON。比方说,我有以下方法: [HttpPost] public dynamic SayHello(string First
我引用了以下网页: https://serverfault.com/questions/153983/sockets-found-by-lsof-but-not-by-netstat 使用Python
我有一个 jQuery 突出显示工具,它可以动态地将一个类添加到一个字符串中。如果一个元素包含单词 Hello World 并且用户搜索'ell',它为单词添加了一个跨度 Hello world 要重
如何在 Iframe 中展开 span 标签。 Directional 我尝试使用此删除 $('iframe').contents().find(".newtext").unwrap(); 如果 re
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 4 年前。 Improve
在我的代码中,我有很多带有 Option 的结构在他们里面。我需要在很多地方与他们一起工作,所以我的代码中充斥着像 car.engine.as_ref().unwrap() 这样的结构访问。 .这对代
这个问题在这里已经有了答案: Unable to read file contents to string - Result does not implement any method in sco
出于某种原因,Rust 编译器提示 Result 没有实现 unwrap,即使我提供的错误类型确实实现了 Debug。下面提供了出错的代码。 use std::fmt::{Display, Debug
多么奇怪的错误: use std::collections::BTreeMap; struct MyStruct1; struct Error; fn get_res() -> Result), Er
我是一名优秀的程序员,十分优秀!