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

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.

主站蜘蛛池模板: 清苑县| 报价| 泉州市| 大英县| 咸阳市| 中西区| 黄龙县| 垫江县| 清河县| 和田市| 鹤庆县| 平顶山市| 屯昌县| 栖霞市| 开原市| 镇远县| 湖北省| 太仆寺旗| 和田市| 繁峙县| 若尔盖县| 亳州市| 且末县| 洛隆县| 静乐县| 竹溪县| 斗六市| 松江区| 淄博市| 新乡县| 台东县| 班玛县| 无锡市| 梨树县| 桐庐县| 邵阳市| 宜君县| 连南| 西平县| 南宁市| 延长县|