Summary of "IOS" swift data types, operators, new statements, etc.

Source: Internet
Author: User

Summarize and review Some of the basic things, mainly with OC different and new.

I. Basic data types

Int, Float, Double, Bool, Character, String (uppercase in first letter)
Array, Dictionary, tuple type (tuple), optional type (Optional)

Specify the data type:

Let A:int = Ten  //Specifies the data type. Generally do not need to specify, will automatically determine the type. (initialization required when used)
1.1 Shaping

1.1 Shaping Let MaxInt = Int.max     //Get maximum value of Int type let minint = Int.minlet A1 = Ten    //decimal Let a2 = 0b1010//  binary start with 0b let A3 = 0o12   //Eight binary starting with 0o let A4 = 0xA    //16 binary 0x

1.2 Floating point

1.2 Floating-point Float,doublelet B1 = 0.123    //default is Doublelet b2 = 0.123e3  //123.0

1.3 Bool, keep up with the mainstream ....

1.3Bool (not yes/no) let C1 = Truelet C2 = False

1.4 Yuan zu

1.4 tuples. can be n any type of data let Tup1 = (age:1,name: "abc", hight:2,jj:0.1)  //With element name lets tup2 = (1, "abc", 2,0.1)   //Omit element name Var Tup3: ( int,string) = (1, "haha")   //Specifies the type. (the element name cannot be written again after the type is specified) var (t1,t2) = Tup3   //directly get 2 variables to receive the tuple Var (_,t3)  = Tup3   //Receive only one

ii. output format of data && type conversion && type alias

Simplifies a lot.

2.1 Additional 0 Or _ can be added to the number, enhanced read-write let D1 = 000123let D2 = 00123.0012300let D3 = 1_2_3//let D4 = _123   //Error

2.2 type conversion Let E1 = 1let e2 = 0.12//let E3 = e1 + e2  //error, different types cannot be added directly let E3 = Double (E1) + E2   //type conversion

The type alias is equivalent to a typedef in C

2.3 Type alias Typealiastypealias xnint = Intlet Test:xnint = 10

Third, the new addition of several operators

3.1 range operator, used on WWDC

3.1 Range Operator: <   index1 in 1...10{}  //means closure [1 10]for index2 in 1..<10{}  //means semi-closed [1 10]

3.2 Overflow operator. (newly added, available to handle data overflow scenarios)

&+ overflow plus &-overflow minus &* overflow multiply &/overflow except &% overflow remainder

Let F1 = Uint.max//let F2 = x + 1//  This write will overflow let F2 = f1 &+ 1   //overflow plus. The value is 0. The lowest value after becoming overflow let G1 = 10//let g2 = g1/0
   //will error let g2 = G1 &/0//   value is 0//let g3 = g1% 0  //error let g3 = G1 &% 0   //value 0

3.3 Some places of attention

Not 0 is really this C language thing is eliminated! The assignment is more flexible, and the assignment has no return value.

Let (x, y) = (UP)    //Overall assignment//if (x = y) {}//    This is wrong. can prevent = = Write =//if (ten) {}     //It is also wrong to write this. Not 0 that is true. if (true) {}

Iv. Process Control Statements

Adds a new for in statement while the switch statement is more flexible.

4.1 Loop for in

For H1 in 1...10{    println (H1)}for _ in 1..<10{    //Do not need to use values in the range, directly ignore with _    println ("# #")}
4.2 tags (can be used to jump out of the specified loop, to understand)
Xn: For    _ in 1...3 {        println ("* * *") for        tmp in 1...5{            println ("# # #")            //Specify exit            if tmp = = 3{                Break xn            }}}        
4.3 switch statement

1) differs from the C language. Do not write break for each case, auto break
2) There must be an executable statement behind each case, or an error will be
3) Each case can match multiple conditions, or you can fill in a range
4) switch to ensure that all cases are handled, with default.
5) Case can also match meta-ancestor

Let score = 10switch score{case 0...3:    println ("poor") Case 4,5,6,7:    println ("good") Case 8...10:    println ("excellent") Default:    println ("Super God")}

Evaluation: indeed simplified a lot, writing is also very handy.


Reference:

The Swift programming Language

Apple Dev Center


Reprint Please specify source: http://blog.csdn.net/xn4545945


Summary of "IOS" swift data types, operators, new statements, etc.

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.