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

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.

主站蜘蛛池模板: 萨嘎县| 蚌埠市| 淳化县| 眉山市| 乳源| 孟连| 楚雄市| 宣恩县| 孝感市| 雷州市| 溧水县| 滦平县| 长顺县| 建阳市| 永济市| 灵山县| 岱山县| 武冈市| 双鸭山市| 宝应县| 宣武区| 邹平县| 砚山县| 磴口县| 乌什县| 商河县| 白山市| 云南省| 灌南县| 内江市| 达日县| 泰顺县| 民和| 呼和浩特市| 若尔盖县| 沈阳市| 庄河市| 龙井市| 四会市| 灌阳县| 和平县|