简体   繁体   English

swift中可选值之间的区别?

[英]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 . 我推荐这篇文章

  1. 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" } 
  2. 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" 
  3. 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 

`var title:String? `var title:String? = "Title"` =“标题”`

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 如果titlenil则上面会崩溃

`var title:String! `var title:String! = "Title"` =“标题”`

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

`var title:String = "Title"` `var title:String =“Title”`

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.

相关问题 Swift中两种可选语法之间的区别 - Difference between two optional syntaxes in Swift Swift中“可选链接”和“可选调用链接”之间的区别 - Difference between “optional chaining” and “optional call chaining” in Swift Swift:在两个可选值之间切换 - Swift: Switch between two optional values Optional和有什么不一样 <T> 和Swift中的可选类型? 扩展可选以携带错误信息? - What's the difference between Optional<T> and optional types in Swift? Extending Optional to carry error information? 快速枚举中关联值和原始值之间的差异 - Difference between associated and raw values in swift enumerations 在Swift 3.0中使用可选和不使用可选来声明非分配变量之间有什么区别 - What is the difference between using optional and not using optional to declare non assign variables in swift 3.0 Swift 结构可选值 - Swift struct optional values 可选和隐式展开的可选之间有什么区别? - What is the difference between optional and implicitly unwrapped optional? Swift将可选元组构造为单个可选值 - Swift destructure an optional tuple to individual optional values 类型Data的deviceToken和Swift-IOS中可选String类型的Fir InstanceID标记有什么区别? - What is the difference between deviceToken of type Data and Fir InstanceID token of type optional String in Swift - IOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM