- Swift 4 Programming Cookbook
- Keith Moon
- 182字
- 2021-07-08 10:21:23
Getting started
Enter the following into the playground:
var dayOfTheWeek: String = "Monday"
dayOfTheWeek = "Tuesday"
dayOfTheWeek = "Wednesday"
dayOfTheWeek = nil
When you try to run the code, you'll see that the compiler has raised an error and will not let you assign nil to the dayOfTheWeek variable. Quite right too--the day of the week might change, but there will never not be a current day of the week.
As we declared the type to be String, that is what the compiler expects, and nil is not a String, so it can't be assigned to this variable.
The same is true even if you remove the type declaration and have the compiler infer it, as we did in the preceding recipe. This is the type inferred at the point the variable is declared, and since it is being assigned a string value, the type of String is inferred. All other uses of this variable are checked against this inferred type of String.
Delete the last line, as the compiler issue will prevent us from running further code in the playground.
- 程序員面試筆試寶典(第3版)
- 零基礎(chǔ)學(xué)Visual C++第3版
- Visual Basic .NET程序設(shè)計(jì)(第3版)
- Expert C++
- 從零開(kāi)始:數(shù)字圖像處理的編程基礎(chǔ)與應(yīng)用
- AIRAndroid應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)
- JSP開(kāi)發(fā)案例教程
- Nexus規(guī)模化Scrum框架
- Java項(xiàng)目實(shí)戰(zhàn)精編
- Oracle 18c 必須掌握的新特性:管理與實(shí)戰(zhàn)
- Windows Phone 7.5:Building Location-aware Applications
- C++寶典
- GameMaker Essentials
- Emgu CV Essentials
- Photoshop CC移動(dòng)UI設(shè)計(jì)案例教程(全彩慕課版·第2版)