解决方法
试试这个
var a:Int? a = 10 print(a)
好…
? (Optional) indicates your variable may contain a nil value while ! (unwrapper) indicates your variable must have a memory (or value) when it is used (tried to get a value from it) at runtime.
主要区别在于,当optional是nil时,可选链接会正常失败,而当optional可选为nil时,强制解包会触发运行时错误.
var defaultNil : Int? // declared variable with default nil value println(defaultNil) >> nil var canBeNil : Int? = 4 println(canBeNil) >> optional(4) canBeNil = nil println(canBeNil) >> nil println(canBeNil!) >> // Here nil optional variable is being unwrapped using ! mark (symbol),that will show runtime error. Because a nil optional is being tried to get value using unwrapper var canNotBeNil : Int! = 4 print(canNotBeNil) >> 4 var cantBeNil : Int = 4 cantBeNil = nil // can't do this as it's not optional and show a compile time error
Here is basic tutorial in detail,by Apple Developer Committee.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。