且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么要创建“隐式展开的可选",因为这意味着您知道存在价值?

更新时间:2023-11-25 21:48:04

考虑一个对象的情况,该对象在构造和配置时可能具有nil属性,但是此后是不可变的且非nil(通常以这种方式对待NSImage ,尽管有时它仍然有用).隐式解开的可选对象将清理其代码,并且安全损失相对较低(只要持有一个保证,它将是安全的).

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.