- 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.
- SPSS數據挖掘與案例分析應用實踐
- The Modern C++ Challenge
- 圖解Java數據結構與算法(微課視頻版)
- Java高手真經(高級編程卷):Java Web高級開發技術
- Raspberry Pi for Secret Agents(Third Edition)
- Python機器學習實戰
- 自然語言處理Python進階
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- C語言開發基礎教程(Dev-C++)(第2版)
- jQuery炫酷應用實例集錦
- NoSQL數據庫原理
- D3.js By Example
- Python深度學習原理、算法與案例
- Go語言開發實戰(慕課版)
- Java程序設計與項目案例教程