[英]Difference between optional values in swift?
What is the difference between: 有什么区别:
var title:String? = "Title" //1
var title:String! = "Title" //2
var title:String = "Title" //3
What am I saying if I were to set title in each way and am I forced to unwrap each variable in a different way? 如果我在各方面设置标题并且我被迫以不同的方式展开每个变量,我该怎么说?
Think about ?
想想
?
and !
而且
!
like a box that might have a value or not. 就像一个可能有价值的盒子。
I recommend this article . 我推荐这篇文章 。
Optional box that might have value or might not , and that optional box is not unwrapped. 可选的框可能有值或可能没有 ,并且可选框不会被解包。
var title:String? = "Title" //second and third picture
You use unwrapped value like that: 您使用这样的展开值:
if let title = title { //do sth with title, here is the same like let title: String = "Title" }
Optional box that might have a value or might not , and that optional box is actually unwrapped. 可选框, 可能有值或可能没有 ,并且可选框实际上是打开的。 If there is a value and you access that value, that is ok ( second image , just replace
?
with !
), but if there is no value, then app crash ( third image , just replace ?
with !
) 如果有值并且您访问该值,那就没问题( 第二张图片 ,只需替换
?
with !
),但如果没有值,则应用程序崩溃( 第三张图片 ,只需替换?
with !
)
var title:String! = "Title"
That variable have a value for sure , and you cannot assign to this value nil
(because it is not optional). 该变量具有肯定的值 ,并且您不能将此值赋值为
nil
(因为它不是可选的)。 Optional means that there is a value or there is no value ( nil
): 可选表示存在值或没有值(
nil
):
var title:String = "Title" //first picture
title
currently has a value of Title
, but in the future it could possibly be nil
. title
目前有一个Title
值,但将来它可能是nil
。 I will need to unwrap it using optional binding: 我需要使用可选绑定来解包它:
if let unwrappedTitle = title {
println(unwrappedTitle)
}
Or by forcing the unwrap with the !
或者通过强制解开
!
character 字符
let unwrappedTitle = title!
The above will crash if title
is nil
如果
title
为nil
则上面会崩溃
title
currently has a value of "Title"
. title
目前的值为"Title"
。 It could possibly be nil, but I know that it never will be when I am using it. 它可能是零,但我知道我使用它时永远不会。 You don't need to unwrap this with optional binding or by forcing the unwrap with the
!
您不需要使用可选绑定或通过强制解包来展开它
!
character. 字符。
Your program will crash if this value is ever accessed while nil
, but the compiler will let you set this value to nil
. 如果在
nil
访问此值,程序将崩溃,但编译器将允许您将此值设置为nil
。
title
currently has a value of "Title"
. title
目前的值为"Title"
。 This may change, but the variable title
will always have some string value. 这可能会改变,但变量
title
将始终具有一些字符串值。 I don't need to check for nil
with optional binding or by forcing an unwrap. 我不需要使用可选绑定或强制解包来检查
nil
。 The compiler will not let you build if you try to set title
to nil
. 如果您尝试将
title
设置为nil
,编译器将不允许您构建。
var title:String? = "Title" //1 Nil Possible-Force Unwrap needed-Nil checking when use
var title1:String! = "Title"//2 Nil Possible-Force Unwrap not needed-Nil cheking when Use
var title2:String = "Title" //3 Nil Not possible as its initailize when declared-No force unwrap needed-No Nil Cheking is needed.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.