- Swift 4 Programming Cookbook
- Keith Moon
- 186字
- 2021-07-08 10:21:22
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.
- 新一代通用視頻編碼H.266/VVC:原理、標準與實現
- 跟“龍哥”學C語言編程
- Java 9 Programming Blueprints
- Object-Oriented JavaScript(Second Edition)
- 精通API架構:設計、運維與演進
- Learning Firefox OS Application Development
- Building Cross-Platform Desktop Applications with Electron
- JS全書:JavaScript Web前端開發指南
- QGIS:Becoming a GIS Power User
- Visual C++開發入行真功夫
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- 移動互聯網軟件開發實驗指導
- 細說Python編程:從入門到科學計算
- HTML5移動Web開發
- 計算思維與Python編程