The difference between OC and swift in interpreting statements

Source: Internet
Author: User

Switch structure in 11.swift

Difference One:

Switch condition in OC can only put integers

The switch condition in Swift can put almost any data type

Difference Two:

There should be a break in each case in OC, and if there is no break, it will continue down through the execution of the box until the break jumps out of the switch structure

There is no need to add a break statement at the end of the case branch in Swift, which jumps out of the switch structure when the case branch is completed

Difference Three:

In OC, not every case must have an executable statement, and if there is no executable statement and break in that branch, it will execute a multi-conditional match by executing the code in the branch that has the most recent executable statement below it.

There must be an executable statement behind each case in Swift,

PS: This difference is due to the existence of the difference between the two, Swift in the case of multi-condition matching is used in the syntax, cases followed by multiple conditions, separated by commas.

In addition, Swift's case can be filled in with a range as a match condition, such as a...b:

Difference Three:

Default in switch in OC is not required

Default in switch in Swift is required

The switch statement in Swift can use where to increase the judging condition

Such as:

var point = (10,-10)

Switch point{

Case let (x, y) where x = = y:

//

Case let (x, y) where x = =-Y:

//

Default:

//

}

The switch statement in Swift can use the Fallthrough keyword to perform a run-through, that is, after the current case is executed, the case that follows Fallthrough executes the default statement, similar to the case in OC without a break statement, With Fallthrough, however, a constant or variable cannot be defined in the following case condition, meaning that the case condition after Fallthrough cannot receive a point using a variable like let (x, y) in the code above, or there will be a syntax error.

The difference between OC and swift in interpreting statements

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.