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

Guard

A guard statement can be used for early exits. We can use a guard statement to require that a condition must be true in order for the code after the guard statement to be executed. The following example presents the guard statement usage:

func greet(person: [String: String]) { 
guard let name = person["name"] else {
return
}
print("Hello Ms\(name)!")
}

In this example, the greet function requires a value for a person's name; therefore, it checks whether it is present with the guard statement, otherwise it will return and not continue to execute. As can be seen from the example, the scope of the guarded variable is not only the guard code block, so we were able to use name after the guard code block in our print statement.

主站蜘蛛池模板: 资中县| 和平县| 雷州市| 乌拉特后旗| 平南县| 修文县| 介休市| 安阳县| 玛曲县| 眉山市| 阿勒泰市| 乐清市| 城步| 兴仁县| 盐津县| 治县。| 石家庄市| 宁河县| 前郭尔| 雷州市| 福鼎市| 水富县| 大冶市| 罗定市| 襄垣县| 闻喜县| 南宫市| 彰化市| 来安县| 淮滨县| 博爱县| 江西省| 建阳市| 玛多县| 六安市| 四平市| 阿拉尔市| 灌阳县| 谷城县| 外汇| 碌曲县|