- 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.
- Python程序設(shè)計(jì)(第3版)
- TypeScript項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)
- jQuery開(kāi)發(fā)基礎(chǔ)教程
- ASP.NET程序設(shè)計(jì)教程
- Python算法詳解
- Scala Data Analysis Cookbook
- Geospatial Development By Example with Python
- 快速入門(mén)與進(jìn)階:Creo 4·0全實(shí)例精講
- 實(shí)戰(zhàn)Java高并發(fā)程序設(shè)計(jì)(第2版)
- Solutions Architect's Handbook
- Visual Basic程序設(shè)計(jì)全程指南
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- SSH框架企業(yè)級(jí)應(yīng)用實(shí)戰(zhàn)
- Docker:容器與容器云(第2版)
- Learning Cocos2d-JS Game Development