- 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.
- Mastering Ext JS(Second Edition)
- 摩登創(chuàng)客:與智能手機(jī)和平板電腦共舞
- PaaS程序設(shè)計(jì)
- The React Workshop
- Python漫游數(shù)學(xué)王國(guó):高等數(shù)學(xué)、線性代數(shù)、數(shù)理統(tǒng)計(jì)及運(yùn)籌學(xué)
- Swift語(yǔ)言實(shí)戰(zhàn)精講
- Raspberry Pi Home Automation with Arduino(Second Edition)
- Learning Laravel's Eloquent
- Python機(jī)器學(xué)習(xí)算法: 原理、實(shí)現(xiàn)與案例
- 時(shí)空數(shù)據(jù)建模及其應(yīng)用
- Instant Debian:Build a Web Server
- Android應(yīng)用開(kāi)發(fā)深入學(xué)習(xí)實(shí)錄
- 小程序從0到1:微信全棧工程師一本通
- Instant Apache Camel Messaging System
- Visual Basic程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)及考試指南