if,guard

You can include multiple optional bindings or multiple boolean conditions in an if statement, as long as they are separated by commas. As long as any optional binding value is nil, or any boolean condition is false, the entire if condition is judged to be false, then you need to use nested if conditional statements to deal with it, as shown below:

if let first = Int("6"), let second = Int("63"), first < second && second < 100 {
    print("\(first) < \(second) < 100")
}
// 输出 "6 < 63 < 100"

if let first = Int("6") {
    if let second = Int("63") {
        if first < second && second < 100 {
            print("\(first) < \(second) < 100")
        }
    }
}
// 输出 "6 < 63 < 100"

Note: Use constants and variables in an if conditional statement to create an optional binding, the value is only available in the body of the if statement. Instead, use constants and variables in the guard statement to create an optional binding where the value is only available outside the guard statement and after the statement

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324888359&siteId=291194637