[IOS] Summary of Swift data types, operators, and new statements, iosswift

Source: Internet
Author: User
Tags integer numbers

[IOS] Summary of Swift data types, operators, and new statements, iosswift

Summarize and review some basic things, which are different from OC and newly added.

I. Basic Data Types

Int, Float, Double, Bool, Character, String (uppercase)
Array, Dictionary, Tuple, and Optional)

Data Type:

Let a: Int = 10 // specify the data type. Generally, you do not need to specify it. The type is automatically determined. (Initialization is required during use)
1.1 integer

// 1.1 integer let maxInt = Int. max // obtain the maximum value of Int type let minInt = Int. minlet a1 = 10 // decimal let a2 = 0b1010 // The binary starts with 0 B. let a3 = 0o12 // octal starts with 0 o. let a4 = 0xA // hexadecimal 0x

1.2 floating point

// 1.2 Float, Doublelet b1 = 0.123 // The default value is Doublelet b2 = 0.123e3 // 123.0

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

// 1.3 Bool (not YES/NO) let c1 = truelet c2 = false

RMB 1.4 million

// Set the value to 1.4. it can be N data of any type. let tup1 = (age: 1, name: "abc", hight: 2, jj: 0.1) // let tup2 = (1, "abc", 2, 0.1) // omit the element name var tup3 :( Int, String) = (1, "haha ") // specify the type. (The element name cannot be written after the specified type is specified.) var (t1, t2) = tup3 // directly obtain two variables to receive the original var (_, t3) = tup3 // only receives one

1.5 (either return this value or nil)

Var possibleNumber = "123" var convertedNumber: Int? = PossibleNumber. toInt () // conversion may fail. (ADD? Optional) // obtain the value of the optional type, which must be added later! If convertedNumber! = Nil {println ("convertedNumber value is \ (convertedNumber !) ")} // Nil in OC points to a non-existent object, while nil in Swift is not a pointer, instead, a default value // possibleNumber = nil // normal variable cannot be assigned a value of nil // convertedNumber = nil // optional type can be assigned a value of nil. If there is no value, automatically assigned to nilif let my = convertedNumber {println ("this is an optional binding! \ (My )")}


Ii. data output format & type conversion & type alias

Simplified a lot.

// 2.1 you can add an extra 0 or _ to the number to enhance 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 directly added. let e3 = Double (e1) + e2 // type conversion

The Type alias is equivalent to typedef in C.

// 2.3 type alias typealiastypealias XNInt = Intlet test: XNInt = 10

3. New operators

3.1 range operators, used in WWDC

// Range operator 3.1 .. <... for index1 in 1... 10 {} // indicates the closure [1 10] for index2 in 1 .. <10 {} // indicates semi-closed [1 10)

3.2 overflow operator. (New ones can be used to handle data overflow)

& + Overflow addition &-overflow subtraction & * overflow multiplication &/overflow addition & % overflow Remainder

Let f1 = UInt. max // let f2 = x + 1 // so that the write will overflow let f2 = f1 & + 1 // overflow. the value is 0. the minimum value after overflow is let g1 = 10 // let g2 = g1/0 // an error is returned. let g2 = g1 &/0 // The value is 0 // let g3 = g1 % 0 // an error is reported. let g3 = g1 & % 0 // The value is 0.

3.3 notes

If the value is not 0, the C language is eliminated! The assignment is more flexible, and the assignment has no return value.

Let (x, y) = (1, 2) // overall assignment // if (x = y) {}// this write is incorrect. it is also wrong to write = As = // if (10. if it is not 0, it is no longer applicable. if (true ){}

Iv. Process Control statements

The for in statement is added, and the switch statement is more flexible.

4.1 loop for in

For h1 in 1... 10 {println (h1)} for _ in 1 .. <10 {// ignore the use of _ println ("####") when the value in the range is not required ("####")}
4.2 tag (this can be used to jump out of a specified loop. For more information, see)
Xn: for _ in 1... 3 {println ("***") for tmp in 1... 5 {println ("###") // specify to exit if tmp = 3 {break xn }}}
4.3 Switch statement

1) it is different from the C language. You do not need to write break in each case.
2) an executable statement must be followed by each case; otherwise, an error is reported.
3) each case can match multiple conditions or enter a range.
4) The switch must add default to handle all the situations.
5) the case can also match the ancestor.

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

Comment: it is indeed simplified and easy to write.


Refer:

The Swift Programming Language

Apple Dev Center


Reprinted please indicate the source: http://blog.csdn.net/xn4545945



What is the data type of the computing objects on both sides of the C language logical operators?

D is the correct answer. Because the two sides of the logical operators are eventually converted to bool values, the bool value is only non-0 (expressed as 1) and 0, so as long as it is not 0, it is converted to 1, therefore, any type of value can be involved in the operation.

1 Data Type of computing objects on both sides of a logical operator __

Select C ~ At least the C language can be used in this way. Non-0 integer numbers and non-0 ASCII characters are considered to be true 0 and 0 ASCII characters are considered false.

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.