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

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.

主站蜘蛛池模板: 拜泉县| 嵩明县| 淄博市| 交城县| 通渭县| 民权县| 白银市| 广丰县| 南投县| 黎川县| 佛山市| 天全县| 唐山市| 龙泉市| 新乡市| 济源市| 合川市| 武冈市| 焦作市| 阿拉善右旗| 汽车| 开远市| 砚山县| 呈贡县| 灌阳县| 衡东县| 广昌县| 天镇县| 墨竹工卡县| 马公市| 襄樊市| 苏尼特左旗| 迁西县| 乐昌市| 陵川县| 吉首市| 莱西市| 洛隆县| 阜新市| 德格县| 辛集市|