Introduction to Swift-Basic type (2)

Source: Internet
Author: User



Now I am also learning swift language, often go to a lot of Apple community and forum, see a lot of strange phenomenon in the circle, found that many people rushed to translate Swift books and release Swift video. The spirit of their exploration of new knowledge I personally admire. But I also feel that the language has not been published for a few days, quickly to translate books and swift video, and then speak Swift language how beautiful and how good. I personally feel that they have not been quiet, to go to the code and study the swift language in the end there, and eager to translate is too hasty.



The main share of today is the bottom half of the basic types of swift. If you are not very clear about the variables and constants in Swift, please go to Swift's introductory article-Basic type (1)



One: Defines a variable of the specified type


format:
   Var variable: type = variable value

    Explanation:
     Define the type of the variable directly (instead of inferring the type of the variable by its value)





Example:


//
// main.swift
// basic type 2


import Foundation



var 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
println ("B = \ (B)")

var C: String = "SWIFT" // directly define the type of variable C as a string
println ("C = \ (C)")


Run results


A=10
B=true
C=SWIFT





Two: Define constants of the specified type


format:

   Let constant: type = constant value

    Explanation:
    定义 directly define the type of the constant (instead of inferring the constant type by the constant value)


Example:


//
// main.swift
// basic type 2


import Foundation



let A: Int = 10 // directly define the type of the constant A as an integer
println ("A = \ (A)")

let B: Bool = true // directly define the type of the constant B as a boolean
println ("B = \ (B)")

let C: String = "SWIFT" // directly define the type of the constant C as a string
println ("C = \ (C)")


Run results


A=10
B=true
C=SWIFT


Note the point:


What is the difference between var A = 10 and var A: Int = 10?

var A = 10 // The compiler infers directly from the value of the variable 10 that the type of variable A is integer

var A: Int = 10 // directly specify that the type of A is integer





Three: Data type



①: The way of the binary representation


Format:

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

Attention:
The defined variable or constant stores a value of decimal.


Example:



1: Decimal expression mode


//
// main.swift
// basic type 2


import Foundation




var A = 10
/ *
1: Variable value 10 without prefix means decimal
2: The value stored in a variable or constant must be decimal
3:10 is directly assigned to variable A in decimal
* /

println ("A = \ (A)")


Run results



a=10



2: Binary representation:


//
// main.swift
// basic type 2


import Foundation


var B = 0b1010
/ *
1: variable value 0b1010 prefix 0b means binary

2: The value stored in a variable or constant must be decimal

3: After the binary 1010 is converted to decimal, it is assigned to the variable B

* /


println ("B = \ (B)")


Run results


b=10


3: Eight-way representation


//
// main.swift
// basic type 2


import Foundation




var C = 0o12
/ *
1: variable value 0o12 prefix is 0o octal
2: The value stored in a variable or constant must be decimal
3: After octal 12 is converted to decimal, it is assigned to the variable C
* /
println ("C = \ (C)")


Run results


c=10


4: Hex


//
// main.swift
// basic type 2


import Foundation




var D = 0xA
/ *
1: variable value 0xA prefix is 0x hexadecimal
2: The value stored in a variable or constant must be decimal
3: After the hexadecimal A is converted to decimal, it is assigned to the variable C
* /
println ("D = \ (D)")


Run results


d=10


②: Shaping the form of transformation



Shaping: Signed and unsigned types



Signed type: Int, Int8, Int32,int64



Unsigned type: UInt, UInt8 Uint32,uint64



Note: If your development environment is 32-bit, then int = Int32



If your development environment is 64-bit, then int = Int64



If you use Mac OS X Yosemite 10.10 and Xcode 6.0 Bate then your system is 64-bit



Example:



Symbolic shaping


//
// main.swift
// basic type 2


import Foundation


var A: Int = 10 // definition signed integer variable

var B: Int8 = 10 // Define a signed 8-bit integer variable

var C: Int32 = 10 // Define a signed 32-bit integer variable

var D: Int64 = 10 // Define a signed 64-bit integer variable

println ("A = \ (A)")
println ("B = \ (B)")
println ("C = \ (C)")
println ("D = \ (D)")


Run results


a=10b=10c=10d=10


Unsigned variable definition


//
// main.swift
// basic type 2


import Foundation


var A: UInt = 10 // definition unsigned integer variable

var B: UInt8 = 10 // Define an unsigned 8-bit integer variable

var C: UInt32 = 10 // Define an unsigned 32-bit integer variable

var D: UInt64 = 10 // Define an unsigned 64-bit integer variable

println ("A = \ (A)")
println ("B = \ (B)")
println ("C = \ (C)")
println ("D = \ (D)")


Run results


a=10b=10c=10d=10


1: The difference between signed and unsigned shaping;



Description: An unsigned defined variable value must be an integer, and a signed variable value can be an integer or a negative number



Watch out.


//
// main.swift
// basic type 2


import Foundation



var A: Int = 10 // Writing correctly: A is a signed integer variable, and the value can be an integer or a negative number

var B: Int = -10 /// Written correctly: B is a signed integer variable, the value can be integer and negative

var C: UInt = 10 // Writing correctly: C is an unsigned integer variable value must be integer

var D: UInt = -10 // Wrong writing C is an unsigned integer variable The value cannot be negative


2: Signed and signed range of values


import Foundation






var 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)")


Run results


uint8min=0,uint8max=255

int8min=-128,int8max=127


Other int,uint range of values and above have been, here is not introduced.



Note: Do not manipulate the range of values when assigning values to UInt8 and int 8.



Example


//
// main.swift
// basic type 2


import Foundation



var 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 exceeding the range of -128 to 127





floating-point type;



Floating-point types are divided into; float, Double



Description: Double value range is greater than float type, and system default is double type



Example:


//
// main.swift
// basic type 2


import Foundation


var A = 12.1 // A is a floating-point type, the type is not explicitly specified, the system considers it to be a Double type by default

var B: Float = 12.1 // Specify that the class is a Float of the floating-point type.

println ("A = \ (A), B = \ (B)")


Run results


//
// main.swift
// basic type 2


import Foundation


var A = 12.0 // A is a floating-point type, the type is not explicitly specified, the system considers it to be a Double type by default

var B: Float = 12.0 // Specify that the class is a Float of the floating-point type.

println ("A = \ (A)")
println ("B = \ (B)")


Run results


a=12.0b=12.0


Example 2


//
// main.swift
// basic type 2


import Foundation


var A = 12.1
var B: Double = 12.1
var C: Float = 12.1

println ("A = \ (A)")

println ("B = \ (B)")

println ("C = \ (C)")


Run results


a=12.1b=12.1c=12.1000003814697


Description



var A = 12.1 and var b:double = 12.1 Output the same result. That is, the double type is the default. and output one decimal.



Problem:


Why the output is c=12.1000003814697  want you to think about it??





Digital readability



Example



Underline (Shaping usage)


//
// main.swift
// basic type 2


import Foundation


var A = 110000
var B = 110_000 // underline can be placed anywhere
var C = 1_100_00
/ *
1: A, B, C output is the same
2: Underscores (_) are used to separate foreign numbers, making the numbers easier to read
3: Underline (_) can be placed N, but can not start with underscore (_)

* /



println ("A = \ (A)")
println ("B = \ (B)")
println ("C = \ (C)")


Run results


a=110000b=110000c=110000





Boolean type:


//
// main.swift
// basic type 2


import Foundation


var A: Bool = true // The value of type bool can only be true false

var B: Bool = false

println ("A = \ (A)")

println ("B = \ (B)") 


Run results


A=trueb=false








I have come back to learn from the Swift language of the knowledge to write to form a series. As a new language, personal understanding inevitably has shortcomings, welcome to give me advice. can also add my QQ 14,360,511,081 discussion, if you have any problem, you can also directly in QQ message sent to me, I saw the first time after you reply



Finally, a summary. Send the mind map as the end of the article






Introduction to Swift-Basic type (2)


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.