Print (" helllo world ")Copy the code
  • You don't have to write the main functionSwift uses the first sentence of globally executable code as a program entry
  • A word ofThe end of the code canOmit the semicolon(;) .More than wordsWhen you write a line of codeYou must use a semicolon(;) separated
  • withvardefinevariable.letdefineconstantThe compiler can automatically infer the types of variables and constants
let a = 10 
let b = 20
var c = a + b
c += 30
Copy the code

# constants

  • You can only assign once
  • Its valueIt is not required to be determined at compile time, but it must be assigned once before it can be used
  • Constants, variables inIt cannot be used until it is initialized
// let age = 10 // let age:Int = 10 let age:Int age = 10 // let age:Int age = 10 Let age age = 10Copy the code
Func getAge() -> Int {return 10} let age = getAge() print(age)Copy the code
Print (age) print(height)Copy the code

# identifier

Identifiers (such as constant names, variable names, function names) can use almost any character

The identifier cannot start with a number and cannot contain special characters such as whitespace characters, tabs, and arrows

Common data types

  • Value type:

Enumeration (enum) : Optional struct: Bool, Int, Float, Double, Character, String, Array, Dictionary, Set

  • Reference type:

Class (class)

Integer type:

UInt8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64 In 32-bit platforms, Int is equivalent to Int32, and in 64-bit platforms, Int is equivalent to the maximum value of the integer Int64: UInt8. Max, UInt8. Min

Floating point type:

Float, 32 bits, precision only 6 bits Double, 64 bits, precision at least 15 bits

Let letFloat: Float = 30.0 let letDouble = 30.0Copy the code

# literal

/// Boolean type let bool = true /// String let String = "hahahaha" /// Character let chat:Character = "A" /// integer /// decimal let intDecimal = 17 /// binary let intBinary = 0b1001 /// octal let intOctal = 0o21 /// hexadecimal let intHexadectmal = 0x11 // float type /// decimal, Equivalent to 1.25e2 /// 0.0125 equivalent to 1.25E-2 let doubleDecimal = 125.0 /// hexadecimal meaning 15x2^2, Equivalent to 60.0 let doubleHexadecimal1 = 0xFp2 /// hexadecimal, meaning 15x2^-2, equivalent to 3.75 let doubleHexadecimal2 = 0xFP-2 // PS: Integer and floating point numbers can be added with additional zeros or underscores for readability /// 100_0000, 1_000_000.000_000_1, 000123.456 /// array let array = [1,3,5,7,9] /// dictionary let dictionary = ["age":18,"height":168,"weight":120]Copy the code

Type conversion

// Int1 and Int2 cannot be added directly, Uint1 :Uint16 = 2000 let Int2:UInt8 = 1 let Int3 = Int1 + Uint16 (Int2) /// = 0.14159 let PI = Double(int) + Double let intPi = int (PI) //Copy the code

# tuples

let http404Error = (404,"Not Found") print("The status code is \(http404Error.0)") let (statusCode,statusMessage) = Http404Error print("The status code is \(statusCode)") let (justTheStatusCode,_) = http404Error let http200Status = (statusCode:200,description:"OK") print("The status code is \(http200Status.statusCode)")Copy the code