官术网_书友最值得收藏!

Optionals, unwrap, and force unwrap

In the real world, we don't always know the answer to a problem, and problems can occur if we blindly assume that someone does. The same is true in programming languages, especially when dealing with external systems that we may not control. In many languages, including Objective-C (until recently), there was no way to indicate that something being declared may not exist at the time you attempt to access it. This would lead to either fragile code that could have broken if a nil unexpectedly found its way in or tests being run all over the code to ensure that a value did exist where it was needed, which added complexity and increased the boilerplate code that had to be written.

The nil or null term is used in programming languages to denote the absence of a value, not to be confused with the number 0 as the value or the empty (zero length) string "", they would be something, this... is nothing. Swift uses nil, and this can be assigned to a variable to remove any value currently assigned, replacing it with nil or nothing.

With a focus on Swift being type-safe and making it easier to write safe code, this ambiguity had to be addressed, and the Swift language does this with the use of optionals.

主站蜘蛛池模板: 偏关县| 荥阳市| 灵丘县| 安多县| 临泽县| 卢龙县| 舞钢市| 柞水县| 荃湾区| 宁晋县| 共和县| 集贤县| 锦州市| 江源县| 托克逊县| 石柱| 克拉玛依市| 托克逊县| 景宁| 九龙坡区| 咸宁市| 龙州县| 莒南县| 宁安市| 于田县| 辽宁省| 城市| 英山县| 苏尼特左旗| 建平县| 错那县| 阳新县| 墨脱县| 连城县| 蓬溪县| 新乐市| 克东县| 濮阳县| 楚雄市| 滦平县| 安龙县|