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

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.

主站蜘蛛池模板: 确山县| 和顺县| 泰宁县| 瑞金市| 凉城县| 克什克腾旗| 彰化县| 台前县| 西丰县| 开阳县| 侯马市| 大埔县| 图片| 建宁县| 青川县| 囊谦县| 杭锦后旗| 鲁山县| 武穴市| 台北市| 邢台市| 广灵县| 三都| 凌源市| 循化| 灵璧县| 江川县| 榆林市| 山西省| 鄂伦春自治旗| 涟源市| 武汉市| 南城县| 彰武县| 宁河县| 鸡泽县| 灵武市| 彭水| 龙里县| 乌拉特前旗| 顺义区|