- 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.
- INSTANT OpenCV Starter
- Beginning C++ Game Programming
- OpenCV實例精解
- Learning Elixir
- Web全棧工程師的自我修養
- HTML5從入門到精通 (第2版)
- Express Web Application Development
- Node.js:來一打 C++ 擴展
- R用戶Python學習指南:數據科學方法
- Unity 2018 Augmented Reality Projects
- 創意UI:Photoshop玩轉APP設計
- Android移動應用開發項目教程
- 深入實踐DDD:以DSL驅動復雜軟件開發
- PowerDesigner 16 從入門到精通
- Android Studio開發實戰:從零基礎到App上線 (移動開發叢書)