Swift Learning (II.)

Source: Internet
Author: User

2. Control Flow:

There are three main types of statements

    • If,switch and the new guard

    • For,while

    • Break,continue

About IF

The conditions in the statement no longer require the use of () packages.

1234 let number = 23ifnumber < 10 {    print("The number is small")}

However, the code behind the judgment execution must be wrapped with {}.

The condition after the if must be a Boolean expression

That is, it is not implicitly compared to 0, which is wrong, because number is not a Boolean expression, number! = 0 is.

123 int number = 0ifnumber{}

About for

The For loop becomes more convenient and more powerful in Swift.

Benefit from Swift's newly added range operator ... and ...<

We are able to use the tedious for loop:

1234 for(int i = 1; i <= 5; i++){    NSLog(@"%d", i);}

Overwrite to:

123 forindex in1...5 {    print(index)}

Although no similar usage is mentioned in the Swift programming Language, we have an elegant approach in swift.

123 forindex instride(from: 1, through: 5, by: 2) {    print(index)}// through是包括5

Then the traversal of the dictionary is enhanced. In the fast enumeration of objective-c we can only enumerate the keys of the dictionary.

1234 NSString *key;for(key insomeDictionary){     NSLog(@"Key: %@, Value %@", key, [someDictionary objectForKey: key]);}

In Swift, through tuple we can enumerate both key and value:

1234 let dictionary = ["firstName":"Mango","lastName":"Fang"]for(key,value) indictionary{    print(key+" "+value)}

About switch:

Swich also gained enhanced functionality and increased security in swift.

No break is required to terminate the next case execution

That is, the following two types of notation are equivalent.

123456789 let character = "a"switchcharacter{    case"a":        print("A")    break    case"b":        print("B")    breakdefault: print("character")
1234567 let character = "a"switchcharacter{    case"a":        print("A")    case"b":        print("B")default: print("character")

This improvement avoids forgetting to write the error caused by the break, I have a deep experience, it was because of the omission of the write-off and spent a period of time to debug.

If we want to treat different values uniformly, separate the values with commas.

1234 switchsome value to consider {casevalue 1,value 2:    statements}

In Swift, the type of switch support has been greatly broadened.

This means that in development we are able to match strings, floating-point numbers, and so on.

Previously, the tedious wording of OC could be improved:

123456789 if([cardName isEqualToString:@"Six"]) {    [self setValue:6];elseif([cardName isEqualToString:@"Seven"]) {    [self setValue:7];elseif([cardName isEqualToString:@"Eight"]) {    [self setValue:8];elseif([cardName isEqualToString:@"Nine"]) {    [self setValue:9];}
12345678910 switchcarName{    case"Six":        self.vaule = 6    case"Seven":        self.vaule = 7    case"Eight":        self.vaule = 8    case"Night":        self.vaule = 9   }

Swift Learning (II.)

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.