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

There's more...

While we defined constants and variables earlier, we also defined the type of thing we are assigning to explicitly. For example, consider the following:

let clearlyAString: String = "This is a string literal" 

Swift is a statically typed language, which means any constant or variable that we define has to have a specific type, which cannot be changed. However, in the preceding line, the clearlyAString constant is clearly a String! The right-hand side of the expression is a String literal, and therefore we know that the left-hand side will be a String. More importantly, the compiler also knows this. Swift is all about being concise, so since the type can be inferred by the compiler, we do not need to explicitly state it. Try the following instead, and see whether you can run the code:

let clearlyAString = "This is a string literal" 

In fact, all the type declaration that we have made so far can be removed! So, go back through the code we have already written and remove all type declarations (:String, :Int, :Float, and :Bool), as they can all be inferred.

主站蜘蛛池模板: 习水县| 句容市| 娄烦县| 奉新县| 高要市| 广东省| 屏山县| 柞水县| 施秉县| 页游| 肥乡县| 蒙山县| 墨脱县| 个旧市| 青冈县| 衢州市| 安西县| 纳雍县| 洛川县| 武宣县| 东方市| 保山市| 涿鹿县| 军事| 莱芜市| 兰考县| 文水县| 宣恩县| 北京市| 峨眉山市| 绥阳县| 巫溪县| 兰坪| 北辰区| 天等县| 新疆| 临洮县| 延川县| 馆陶县| 竹山县| 塔城市|