Differences between OC and Swift 3. Differences between OCSwift 3
11. switch structure in swift
Difference 1:
The switch condition in oc can only be an integer.
The switch condition in swift can be set to almost any data type.
Difference 2:
Each case in oc should have a break. If there is no break, it will continue to run down the case until the break jumps out of the switch structure.
Swift does not need to add the break statement at the end of the case Branch. After the case Branch is executed, it will jump out of the switch structure.
Difference 3:
In oc, not every case must be followed by executable statements. If a case Branch does not have executable statements and break, the Branch will execute the code in the branch that has the most recent executable statement below it to implement multi-condition matching.
Every case in swift must be followed by executable statements,
Ps: the difference is due to the existence of difference 2. In swift, the syntax used to indicate multi-condition matching is case followed by multiple conditions, separated by commas.
In addition, a range can be filled in swift case as a matching condition, such as case a... B:
Difference 3:
Default in switch in oc is not required
Default is required in switch in swift.
The switch statement in swift can use where to add judgment conditions.
For example:
Var point = (10,-10)
Switch point {
Case let (x, y) where x = y:
//
Case let (x, y) where x =-y:
//
Default:
//
}
In swift, the switch statement can use the fallthrough keyword to execute the throughout operation. That is, after the current case is executed, the default statement is executed for the case after fallthrough, similar to the case statement not written in oc, however, if fallthrough is used, the subsequent case conditions cannot define constants or variables. That is, the case conditions after fallthrough cannot use let (x, y) similar to the above Code) such a variable to receive the point, otherwise there will be a syntax error.