Swift中Optional(可选项)的理解

564 阅读3分钟

为什么会有Optional

OC中没有Optional类型,OC中所有对象变量都可以为nil,因为nil是无类型的指针。在OC中字典、数组、集合都不能放入nilnil只能用在OC对象上面,变量在一定程度上来讲便利性较差,但在Swift中却不同。Swift中nil和OC中的nil是有很大区别的。在OC中nil是指向一个不存在的对象的指针,但是在Swift中,nil不是指针,只是值缺失的特殊类型,任何类型可选项都可以设置为nil。所以在Swift中,可以用可选项值为nil,来表达变量的值缺失,增加了一定的便利性。 Swift中我们在变量类型后面添加 ?来表示一个可选项,例如:

var name: String? = nil

Optional的实现

Optional其实是一个枚举类型,我们查看标准库中代码可以看到

@frozen public enum Optional<Wrapped> : ExpressibleByNilLiteral {

    /// The absence of a value.
    ///
    /// In code, the absence of a value is typically written using the `nil`
    /// literal rather than the explicit `.none` enumeration case.
    case none

    /// The presence of a value, stored as `Wrapped`.
    case some(Wrapped)

    /// Creates an instance that stores the given value.
    public init(_ some: Wrapped)

这个枚举有两个值,代码Optional的两层意思

  • none 代表变量没有值,即为nil
  • some 代表变量有值,值为somesome包装了实际了值

那Optional是如果得到实际的值呢,还是来看标准库中的代码,

    /// The wrapped value of this instance, unwrapped without checking whether
    /// the instance is `nil`.
    ///
    /// The `unsafelyUnwrapped` property provides the same value as the forced
    /// unwrap operator (postfix `!`). However, in optimized builds (`-O`), no
    /// check is performed to ensure that the current instance actually has a
    /// value. Accessing this property in the case of a `nil` value is a serious
    /// programming error and could lead to undefined behavior or a runtime
    /// error.
    ///
    /// In debug builds (`-Onone`), the `unsafelyUnwrapped` property has the same
    /// behavior as using the postfix `!` operator and triggers a runtime error
    /// if the instance is `nil`.
    ///
    /// The `unsafelyUnwrapped` property is recommended over calling the
    /// `unsafeBitCast(_:)` function because the property is more restrictive
    /// and because accessing the property still performs checking in debug
    /// builds.
    ///
    /// - Warning: This property trades safety for performance.  Use
    ///   `unsafelyUnwrapped` only when you are confident that this instance
    ///   will never be equal to `nil` and only after you've tried using the
    ///   postfix `!` operator.
    @inlinable public var unsafelyUnwrapped: Wrapped { get }

它是一个定义的get方法,Optionl通过unsafelyUnwrapped来获取实际的值,例如:

var ddb: String? = "冬冬吧"
let ddbCount = ddb.unsafelyUnwrapped.count

这样就得到了变量的实际值。

Optional的使用

实现一个Optional
let ddb: Optional<String> = "冬冬吧"
// var ddb: String? = "冬冬吧"

我们这样实现的可选项,实际上和注释部分的类型后面加?实现的是完全一样的。

可选项的解包

可选项是不能直接使用的,需要解包后才能使用,基本上有一下解包方式

  • !强制解包,例如:
let count = ddb!.count

在强制解包前,你如果不知道它是否为nil,那你需要先对它进行非nil的判断保护,否则强制解包一旦失败,程序会报错,如下代码:

if ddb != nil {
    let count = ddb!.count
    print(count)
}

这样即使我们使用了强制解包,但它的运行依然是安全的

  • if判断展开,例如:
if ddb != nil {
    let count = ddb?.count
    print(count ?? 0)
}

这里我们使用a ?? b 合并空值运算符的方式来解包,如果有值,则为count,如果为nil,则默认0 使用合并控制运算符有两个条件: 1.表达式a必须是可选类型 2.表达式b必须和a的处处类型相同

  • 使用可选项绑定的方式
if let ddbStr = ddb {
    let count = ddbStr.count
    print(count)
}

使用可选项绑定来判断可选项是否有值,如果有就赋值给临时变量。同一个if语句可以有多个可选项绑定,用, 分开即可

小结

Optional,是”不存在“或“空”概念的加强版本。而nil则是“不存在”的基础版本 在swift中引入optional的目的,就是将"不存在"这个概念绑定到具体的类型上。optional.nil指向的是值的“不存在”,同时表示:如有值只能是optional.some<T>中的T类型,将所有类型的值空间进行了nil的扩展。