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

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.

主站蜘蛛池模板: 文水县| 云阳县| 海安县| 锦州市| 彭水| 安庆市| 叙永县| 舒兰市| 纳雍县| 綦江县| 丽江市| 札达县| 山东| 三门县| 太原市| 宜良县| 永昌县| 谢通门县| 泸西县| 塔城市| 清河县| 营口市| 白城市| 同江市| 阿勒泰市| 西青区| 邯郸市| 石嘴山市| 弥渡县| 嘉善县| 龙里县| 广宁县| 奉贤区| 车致| 集贤县| 庆城县| 太仓市| 台前县| 岳普湖县| 齐齐哈尔市| 皋兰县|