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

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.

主站蜘蛛池模板: 永和县| 宜州市| 怀来县| 永清县| 迁安市| 交口县| 万全县| 渭南市| 拉萨市| 虹口区| 库伦旗| 阿鲁科尔沁旗| 巴青县| 黎川县| 乌拉特前旗| 博客| 衡东县| 健康| 柳江县| 曲阜市| 亚东县| 应城市| 海兴县| 当阳市| 江城| 南阳市| 峨山| 云霄县| 房山区| 阿勒泰市| 肥城市| 治多县| 乌兰察布市| 湖南省| 彩票| 云浮市| 崇州市| 格尔木市| 富源县| 西乡县| 晋州市|