Difference between Force Unwrapping Optionals and Implicitly Unwrapped Optionals

前端 未结 4 1996
既然无缘
既然无缘 2020-12-13 05:22

I was very confused about forced unwrapping and implicit unwrapping at first. Now, the following understanding comes from my self-study:

There is no

4条回答
  •  难免孤独
    2020-12-13 05:34

    An implicitly unwrapped optional is a normal optional behind the scenes, but can also be used like a non optional value, so yes you are correct.

    But if you declare a value as implicitly unwrapped, it is equivalent to force unwrapping it at every use.

    For Implicitly unwrapped Optionals there are 4 main reasons to do that.

    1: A Constant That Cannot Be Defined During Initialization

    2: Interacting with an Objective-C API

    3: When Your App Cannot Recover From a Variable Being nil

    4: NSObject Initializers

提交回复
热议问题