Differences between OC and Swift 3. Differences between OCSwift 3

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.