Why create “Implicitly Unwrapped Optionals”, since that implies you know there's a value?

前端 未结 10 1796
深忆病人
深忆病人 2020-11-22 07:30

Why would you create a \"Implicitly Unwrapped Optional\" vs creating just a regular variable or constant? If you know that it can be successfully unwrapped then why create a

10条回答
  •  深忆病人
    2020-11-22 08:01

    Consider the case of an object that may have nil properties while it's being constructed and configured, but is immutable and non-nil afterwards (NSImage is often treated this way, though in its case it's still useful to mutate sometimes). Implicitly unwrapped optionals would clean up its code a good deal, with relatively low loss of safety (as long as the one guarantee held, it would be safe).

    (Edit) To be clear though: regular optionals are nearly always preferable.

提交回复
热议问题