[英]Use of an optional value in Swift
While reading the The Swift Programming Language , I came across this snippet: 在阅读The Swift Programming Language时 ,我遇到了这个片段:
You can use if and let together to work with values that might be missing.
您可以使用if和let一起使用可能缺少的值。 These values are represented as optionals .
这些值表示为选项 。 An optional value either contains a value or contains nil to indicate that the value is missing.
可选值包含值或包含nil以指示缺少值。 Write a question mark (?) after the type of a value to mark the value as optional.
在值的类型后面写一个问号(?)以将值标记为可选。
// Snippet #1
var optionalString: String? = "Hello"
optionalString == nil
// Snippet #2
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \(name)"
}
Snippet #1 is clear enough, but what is happening in the Snippet #2? Snippet#1足够清晰,但是在Snippet#2中发生了什么? Can someone break it down and explain?
有人可以分解并解释吗? Is it just an alternative to using an
if - else
block? 它只是使用
if - else
块的替代方法吗? what is the exact role of let
in this case? 在这种情况下,
let
的确切作用是什么?
I did read this page, but still a little confused. 我确实看过这个页面,但还是有点困惑。
if let name = optionalName {
greeting = "Hello, \(name)"
}
This does two things: 这有两件事:
it checks if optionalName
has a value 它检查
optionalName
是否有值
if it does, it "unwraps" that value and assigns it to the String called name
(which is only available inside of the conditional block). 如果是,它将“解包”该值并将其分配给名为
name
的String(仅在条件块内部可用)。
Note that the type of name
is String
(not String?
). 请注意,
name
的类型是String
(不是String?
)。
Without the let
(ie with just if optionalName
), it would still enter the block only if there is a value, but you'd have to manually/explicitly access the String as optionalName!
如果没有
let
(即只if optionalName
),它只有在有值的情况下才会进入块,但是您必须手动/显式访问String作为optionalName!
. 。
// this line declares the variable optionalName which as a String optional which can contain either nil or a string.
//We have it store a string here
var optionalName: String? = "John Appleseed"
//this sets a variable greeting to hello. It is implicity a String. It is not an optional so it can never be nil
var greeting = "Hello!"
//now lets split this into two lines to make it easier. the first just copies optionalName into name. name is now a String optional as well.
let name = optionalName
//now this line checks if name is nil or has a value. if it has a value it executes the if block.
//You can only do this check on optionals. If you try using greeting in an if condition you will get an error
if name{
greeting = "Hello, \(name)"
}
String?
is a boxed-type, variable optionalName
either contains a String
value or nothing(that is nil
). 是一个盒装类型,变量
optionalName
要么包含String
值,要么包含任何值(即nil
)。
if let name = optionalName
is an idiom, it unboxes the value out of optionalName
and assign it to name
. if let name = optionalName
是一个习惯用法,它会将该值从optionalName
取消装箱并将其分配给name
。 In the meanwhile, if the name is non-nil, the if
branch is executed, otherwise the else
branch is executed. 同时,如果名称为非nil,则执行
if
分支,否则执行else
分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.