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

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.

主站蜘蛛池模板: 伊宁县| 长沙县| 湘潭市| 壶关县| 潮州市| 措勤县| 陈巴尔虎旗| 嘉善县| 共和县| 皮山县| 万源市| 泌阳县| 沂水县| 全南县| 土默特右旗| 诏安县| 应用必备| 平果县| 凌源市| 平昌县| 漠河县| 新化县| 丹巴县| 山阳县| 修水县| 遵义市| 河曲县| 高邮市| 龙胜| 集贤县| 千阳县| 陈巴尔虎旗| 江口县| 连平县| 溆浦县| 南京市| 杭锦后旗| 新晃| 渝北区| 琼海市| 沐川县|