Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift Optionals & Implicitly Unwrapped Optionals大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

以下内容摘自: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_ProgrAMMing_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309 As described above, optionals inDicate

以下内容摘自:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309

@H_262_20@As described above,optionals inDicate that a constant or variable is allowed to have “no value”. Optionals can be checked with anifstatement to see if a value exists,and can be conditionally unwrapped with optional binding to access the optional’s value if it does exist.@H_489_23@

// optional 指明一个常量或变量可以没有值。可以通过if 语句来查看值是否存在,如果值存在的话可以通过额外的opTinal binding来获取到这个值。用?来定义@H_489_23@

@H_262_20@Sometimes it is clear from a program’s structure that an optional willalways@H_489_23@have a value,after that value is first set. In these cases,it is useful to remove the need to check and unwrap the optional’s value every time it is accessed,because it can be safely assumed to have a value all of the time.@H_489_23@

// 有时在程序的结构中当这个值被先设置后,这个opTinal总是明确有一个值。这种情况下就不需要每次再去检查,取值了,因为有值的话就是安全的。@H_489_23@

// implicitly unwrapped optional 用!来定义@H_489_23@

@H_262_20@these kinds of optionals are defined asimplicitly unwrapped optionals@H_489_23@. you write an implicitly unwrapped optional by placing an exclamation mark (String!) rather than a question mark (String?) after the type that you want to make optional.@H_489_23@

@H_262_20@// implicitly unwrapped optional 最原始的使用是在类的初始化中 =等下回来update=@H_489_23@@H_489_23@

@H_262_20@// 来看下这两者使用的区别@H_489_23@@H_489_23@

@H_262_20@// 直接将implicitly unwrapped optional看作是 给了optional权限 每次使用自动获取到数据@H_489_23@@H_489_23@

@H_262_20@@H_489_23@

<span style="color:#414141;">let possibleString: String? = "An optional String."
let forcedString: String = possibleString! // requires an exclamation mark
 
let assumedString: String! = "An implicitly unwrapped optional String."
let implicitString: String = assumedString // </span><span style="color:#ff0000;">no need for an exclamation mark</span>

// 要是这个implicitly unwrapped optional没有值的话,直接去获取的话就会出错 -?你妈 什么鬼 什么情况会出现 都确认一定有值了@H_489_23@

// @H_489_23@那么这个implicitly unwrapped optional存在的意义是什么呢??????@H_489_23@

// 我目前看到的都是@IBOutlet中的UILable啊什么的是加了! ->>如果你在有些地方@H_489_23@,不希望它没有值@H_489_23@,你就给他设置!,这样就能很快fail和crash@H_489_23@

// 你可以将implicitly unwrapped optional看作是正常的optional@H_489_23@

@H_262_20@@H_489_23@

if assumedString != nil {
println(assumedString)
}
// prints "An implicitly unwrapped optional String."


// 你可以可以使用optional binding,来检查和获取它的值@H_489_23@

if let definiteString = assumedString {
println(definiteString)
}
// prints "An implicitly unwrapped optional String."

大佬总结

以上是大佬教程为你收集整理的Swift Optionals & Implicitly Unwrapped Optionals全部内容,希望文章能够帮你解决Swift Optionals & Implicitly Unwrapped Optionals所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。