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:

[JavaScript]View Plaincopy
    1. 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

[JavaScript]View Plaincopy
    1. 1.1 Shaping
    2. Let maxInt = Int.max //Get maximum value of type Int
    3. Let minint = Int.min
    4. Let A1 = Ten //Decimal
    5. Let A2 = 0b1010 //binary starts with 0b
    6. Let a3 = 0o12 //Eight binary starting with 0o
    7. Let A4 = 0xA //16 binary 0x


1.2 Floating point

[JavaScript]View Plaincopy
    1. 1.2 Floating-point float,double
    2. Let B1 = 0.123 //default is double
    3. Let B2 = 0.123e3 //123.0


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

[JavaScript]View Plaincopy
    1. 1.3Bool (not yes/no)
    2. Let C1 = true
    3. Let C2 = false

1.4 Yuan zu

[JavaScript]View Plaincopy
  1. 1.4 tuples. can be n any type of data
  2. Let Tup1 = (age:1,name:"abc", hight:2,jj:0.1) //With element name
  3. Let tup2 = (1,"abc", 2,0.1) //Omit element name
  4. var tup3: (int,string) = (1,"haha") //Specifies the type. (the element name cannot be written again after the type is specified)
  5. var (t1,t2) = Tup3 //directly get 2 variables to receive the meta-ancestor
  6. var (_,t3) = Tup3 //Receive only one


II. output format of data && type conversion && type Alias

Simplifies a lot.

[JavaScript]View Plaincopy
    1. 2.1 Additional 0 Or _ can be added to the number to enhance read/write
    2. Let D1 = 000123
    3. Let D2 = 00123.0012300
    4. Let D3 = 1_2_3
    5. Let D4 = _123//Error

[JavaScript]View Plaincopy
    1. 2.2 Type Conversions
    2. Let e1 = 1
    3. Let e2 = 0.12
    4. Let E3 = e1 + E2//error, different types cannot be added directly
    5. Let E3 = Double (E1) + E2 //type conversion


The type alias is equivalent to a typedef in C

[JavaScript]View Plaincopy
    1. 2.3 Type alias Typealias
    2. Typealias Xnint = Int
    3. Let Test:xnint = 10


Third, the new addition of several operators

3.1 range operator, used on WWDC

[JavaScript]View Plaincopy
    1. 3.1 Range Operator: < ...
    2. For index1 in 1...10{} //means closure [1]
    3. For index2 in 1..<10{} //= semi-closed [1]


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

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

[JavaScript]View Plaincopy
    1. Let F1 = Uint.max
    2. Let F2 = x + 1//This will overflow the writing
    3. Let F2 = f1 &+ 1 //overflow plus. The value is 0. The minimum value after becoming overflow
    4. Let G1 = 10
    5. Let G2 = g1/0//Will error
    6. Let g2 = G1 &/0 //value is 0
    7. Let g3 = G1% 0//Will error
    8. Let g3 = G1 &% 0 //value is 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.

[JavaScript]View Plaincopy
    1. Let (x, y) = ( min)//Overall assignment
    2. if (x = y) {}//It is wrong to write this. can prevent = = Write =
    3. if (10) {}//It is also wrong to write. Non-0 is really no longer applicable.
    4. if (true) {}


Iv. Process Control Statements

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

4.1 Loop for in

[JavaScript]View Plaincopy
    1. For H1 in 1...10{
    2. println (H1)
    3. }
    4. For _ in 1..<10{ //Do not need to use the value in the range, directly ignore the _
    5. println ("# # #")
    6. }

4.2 tags (can be used to jump out of the specified loop, to understand)

[Java]View Plaincopy
  1. Xn
  2. For _ in 1 ... 3 {
  3. println ("* * *")
  4. For tmp in 1 ... 5{
  5. println ("# # #")
  6. //Specify Exit
  7. if tmp = = 3{
  8. Break xn
  9. }
  10. }
  11. }

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

[Java]View Plaincopy
  1. Let score = Ten
  2. Switch score{
  3. Case 0 ... 3:
  4. println ("poor")
  5. Case 4,5,6,7:
  6. println ("Liang")
  7. Case 8 ... Ten:
  8. println ("excellent")
  9. Default
  10. println ("Super God")

IOS Swift data types, operators, new statements, and more

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.