Quick Start-basic type (2)

Source: Internet
Author: User

Now I am also learning the Swift language. I often visit many apple communities and forums and see many strange phenomena in the circle. I find many people are busy translating Swift books and publishing Swift videos. I personally admire their spirit of exploring new knowledge. However, I also feel that the language has not been released for a few days, so I am eager to translate books and videos, and then talk about how beautiful and good the Swift language is. I personally think they are not calm down, so it is too hasty to think about code and study the Swift language.

Today we are mainly sharing the lower half of the basic type in swift. If you are not clear about the variables and constants in SWift, go to Quick Start-basic type (1)

1. Define variables of the specified type

Format: var variable: TYPE = variable value explanation: directly defines the type of the variable (instead of inferring the variable type through the variable value)

 

Example:

//// Main. swift // basic type 2 import Foundationvar A: Int = 10 // directly define the type of variable A as integer println ("A = \ (A)") var B: bool = true // directly define the type of variable B as boolean type println ("B = \ (B)") var C: string = "SWIFT" // directly define the type of variable C as the String type println ("C = \ (C )")

Running result

A=10B=trueC=SWIFT

 

2. Define constants of the specified type

Format: let constant: TYPE = constant value interpretation: directly defines the type of a constant (instead of inferring the constant type through the constant value)

Example:

//// Main. swift // basic type 2 import Foundationlet A: Int = 10 // directly define the type of constant A as integer println ("A = \ (A)") let B: bool = true // directly define the type of constant B as boolean type println ("B = \ (B)") let C: string = "SWIFT" // directly define the type of constant C as the String type println ("C = \ (C )")

Running result

A=10B=trueC=SWIFT

Note:

What is the difference between var A = 10 and var A: Int = 10? Var A = 10 // the compiler uses the variable value 10 to deduce that the type of variable A is an integer var A: Int = 10 // directly specify that the type of A is an integer.

 

Iii. Data Types

①: Hexadecimal Representation


Format:

Decimal number, no prefix binary number, with 0b as the prefix octal number, with 0o as the prefix hexadecimal number, with 0x as the prefix

Note:
The defined variable or constant is stored in decimal.

Example:

1: decimal representation

//// Main. swift // basic type 2 import Foundationvar A = 10/* 1: variable value 10 no prefix indicates decimal 2: the value stored in A variable or constant must be in decimal format. It is directly assigned to the variable A */println ("A = \ (A)") in decimal format )")

Running result

A = 10

2: binary representation:

/// Main. swift // basic type 2 import Foundationvar B = 0b1010
/* 1: The variable value 0b1010 prefix 0b indicates binary 2: The value stored in the variable or constant must be in decimal 3: After the binary 1010 is converted to decimal, assign a value to variable B */println ("B = \ (B )")

Running result

B=10

3: octal Representation

//// Main. swift // basic type 2 import Foundationvar C = 0o12/* 1: variable value 0o12 prefix is 0o octal 2: variable or constant stored value must be in decimal 3: after octal 12 is converted to decimal, assign the value to the variable C */println ("C = \ (C )")

Running result

C=10

4: hexadecimal

//// Main. swift // basic type 2 import Foundationvar D = 0xA/* 1: variable value 0xA prefix is 0x hexadecimal 2: variable or constant stored value must be in decimal 3: after hexadecimal A is converted to decimal, the value is assigned to the variable C */println ("D = \ (D )")

Running result

D=10

②: Shaping the form of monetization

Integer: signed and unsigned type

Signed type: Int, Int8, Int32, Int64

Unsigned type: UInt, UInt8 UInt32, UInt64

NOTE: If your development environment is 32-bit, Int = Int32

If your development environment is 64-bit, Int = Int64

If you use mac OS X Yosemite 10.10 and Xcode 6.0 Bate, your system is 64-bit.

Example:

Signed integer

//// Main. swift // basic type 2 import Foundationvar A: Int = 10 // defines the signed integer variable var B: Int8 = 10 // defines the signed 8-bit integer variable var C: int32 = 10 // defines the signed 32-bit integer variable var D: Int64 = 10 // defines the signed 64-bit integer variable println ("A = \ () ") println (" B = \ (B) ") println (" C = \ (C) ") println (" D = \ (D )")

Running result

A=10B=10C=10D=10

Unsigned variable definition

//// Main. swift // basic type 2 import Foundationvar A: UInt = 10 // defines the unsigned integer variable var B: UInt8 = 10 // defines the unsigned 8-bit integer variable var C: UInt32 = 10 // defines the unsigned 32-bit integer variable var D: UInt64 = 10 // defines the unsigned 64-bit integer variable println ("A = \ () ") println (" B = \ (B) ") println (" C = \ (C) ") println (" D = \ (D )")

Running result

A=10B=10C=10D=10

1: The difference between signed and unsigned shaping;

Note: The value of an unsigned variable must be an integer. The variable value can be an integer or a negative number.

Notes

//// Main. swift // basic type 2 import Foundationvar A: Int = 10 // The statement is correct: A is A signed integer variable and the value can be an integer or A negative var B: int =-10 // correct syntax: B is a signed integer variable. The value can be an integer or a negative number. var C: UInt = 10 // The syntax is correct: c: the value of the unsigned integer variable must be var D: UInt =-10 // incorrect Writing C: the value of the unsigned integer variable cannot be negative

2: value range of symbols and symbols

Import Foundationvar UInt8Min = UInt8.min // UInt8.min is the minimum value of UInt8 var UInt8Max = UInt8.max // UInt8.max is the maximum value of UInt8 println ("UInt8Min = \ (UInt8Min ), UInt8Max = \ (UInt8Max )") var Int8Min = Int8.min // UInt8.min is the minimum value of UInt8 var Int8Max = Int8.max // UInt8.max is the maximum value of UInt8 println ("Int8Min = \ (Int8Min), Int8Max = \ (Int8Max) ")

Running result

UInt8Min=0,UInt8Max=255

Int8Min=-128,Int8Max=127

The value range of other Int and UInt types is not described here.

Note: When assigning values to UInt8 and Int 8, do not set the value range.

Example

//// Main. swift // basic type 2 import Foundationvar A: Int8 = 10 // the correct value of A is 10 in the range of-128 to 127 var B: Int8 = 300 // error; the value of B is 300 and exceeds-128 to 127.

 

Floating point type;

Float and Double

Description: The value range of Double type is greater than that of Float type, and the default value is Double type.

Example:

//// Main. swift // basic type 2 import Foundationvar A = 12.1 // A is A floating point type, and the type is not explicitly specified. The system considers it as A Double type var B by default: float = 12.1 // specify the class as the Float type in the floating point type. Println ("A = \ (A), B = \ (B )")

Running result

//// Main. swift // basic type 2 import Foundationvar A = 12.0 // A is A floating point type, and the type is not explicitly specified. The system considers it as A Double type var B by default: float = 12.0 // specify the class as the Float type in the floating point type. Println ("A = \ (A)") println ("B = \ (B )")

Running result

A=12.0B=12.0

Example 2

//// Main. swift // basic type 2 import Foundationvar A = 12.1var B: Double = 12.1var C: Float = 12.1 println ("A = \ ()") println ("B = \ (B)") println ("C = \ (C )")

Running result

A=12.1B=12.1C=12.1000003814697

Note:

Var A = 12.1 and var B: Double = 12.1 have the same output result. That is, the default value is double. And outputs a decimal number.

Problem:

Var C: flowat = 12.1
Why is C = 12.1000003814697 output ??

 

Digit readability

Example

Underline (integer usage)

//// Main. swift // basic type 2 import Foundationvar A = 111_var B = 110_000 // underline can be placed anywhere var C = 127100_00/* 1: A, B, c. The output result is the same. 2: Use an underscore (_) to separate external numbers, which facilitates the readability of numbers. 3: You can place N underscores (_), but not underscores (_) start with */println ("A = \ (A)") println ("B = \ (B)") println ("C = \ (C )")

Running result

A=110000B=110000C=110000

 

Boolean Type:

//// Main. swift // basic type 2 import Foundationvar A: Bool = true // The bool type value can only be true falsevar B: Bool = falseprintln ("A = \ ()") println ("B = \ (B )")

Running result

A=trueB=false

 

 

I wrote the swift language I learned to form a series. As it is a new language, my personal understanding is inevitable. You are welcome to give me some comments. You can also add QQ 1436051108 for discussion. If you have any questions, you can also send a message to me via QQ. I will reply to you immediately after seeing it.

Summary. Send a mind map to end the 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.