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

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.

主站蜘蛛池模板: 水城县| 河津市| 新巴尔虎右旗| 江都市| 竹溪县| 十堰市| 闸北区| 定结县| 隆回县| 古田县| 紫云| 衡南县| 东辽县| 门源| 金阳县| 彰武县| 吴堡县| 松阳县| SHOW| 名山县| 清涧县| 六安市| 清镇市| 托克逊县| 江津市| 乌海市| 桐梓县| 嘉峪关市| 阿鲁科尔沁旗| 博客| 福泉市| 运城市| 隆尧县| 额尔古纳市| 兰西县| 颍上县| 图们市| 福清市| 无极县| 罗城| 澎湖县|