In Apple Swift, in what case(s) would I not want an implicitly unwrapped optional?

前端 未结 3 1641
时光取名叫无心
时光取名叫无心 2021-01-18 08:47

I believe I understand why optionals are handy (my best thought for use is to be able to return a nil Boolean value), but in what case would I want to declare a wrapped opti

3条回答
  •  轮回少年
    2021-01-18 09:17

    If you try to access the content of an implicitly unwrapped optional and there's nothing there, your app will crash.

    If you use the patterns for checking the content of an optional — like optional binding and optional chaining - you can control how your app should fail gracefully in unforeseen situations. And it doesn't make your code all that more complicated.

    Not crashing seems like a good reason to me.

提交回复
热议问题