官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 顺义区| 山阴县| 衡东县| 革吉县| 白水县| 辽宁省| 马尔康县| 新巴尔虎右旗| 东乌珠穆沁旗| 中宁县| 蓬溪县| 千阳县| 额尔古纳市| 古浪县| 宁河县| 宝山区| 奉节县| 海南省| 凯里市| 高陵县| 崇礼县| 江都市| 吉水县| 扬州市| 黄大仙区| 佛教| 金乡县| 丹巴县| 丰都县| 常德市| 襄樊市| 姚安县| 丰原市| 堆龙德庆县| 麻栗坡县| 沁阳市| 浮山县| 聂拉木县| 榆树市| 乐至县| 桦川县|