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

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.

主站蜘蛛池模板: 长泰县| 财经| 吉木萨尔县| 太白县| 禹州市| 巴彦淖尔市| 洛阳市| 苏尼特右旗| 斗六市| 武胜县| 丹阳市| 临泽县| 霞浦县| 介休市| 哈密市| 渭源县| 莱西市| 桦甸市| 巫溪县| 扎兰屯市| 广宁县| 麻阳| 盘锦市| 湛江市| 东乡族自治县| 禹州市| 韶山市| 开阳县| 嘉峪关市| 屯留县| 两当县| 威信县| 思南县| 周口市| 江永县| 庄浪县| 广宁县| 建阳市| 杭锦后旗| 阜新| 武冈市|