One: Swift basic data type
- Data types in Swift also include: integer/Float/object type/struct type, etc.
- Understanding integer and floating-point types first
- Integral type
- have symbols
- Int8: Signed 8-bit integer
- Int16: Signed 16-bit integer
- Int32: Signed 32-bit integer
- Int64: Signed 64-bit integer
- Int: Peace table related (default, Nsinteger equivalent to OC)
- No sign
- UInt8: Unsigned 8-bit integer
- UInt16: Unsigned 16-bit integer
- UInt32: Unsigned 32-bit integer
- UInt64: Unsigned 64-bit integer
- UINT: The Peace Table (commonly used, equivalent to OC Nsuinteger) (default)
- Floating point Type
- float:32 bit floating point type
- double:64 floating-point type (default)
//defines a variable m of type int, and is assigned 10var M: int = 10//defines a double type of constant N, and assigned a value of 3.14let N: double = 3.14 (Same as OC Swift Default floating-point data type is
Double
II: Swift type deduction
- Swift is a strong type of language
- Any identifier in Swift has a definite type
- Attention:
- If an identifier is defined with a direct assignment, the type following the identifier can be omitted.
- Because Swift has a type deduction, it automatically determines the data type of the preceding identifier based on the subsequent assignment
- You can
option
鼠标左键
see the data type of the variable by +
// 定义变量时没有指定明确的类型,但是因为赋值给i一个20.20为整型.因此i为整型var i = 20// 错误写法:如果之后赋值给i一个浮点型数值,则会报错// i = 30.5// 正确写法var j = 3.33j = 6.66
Three: operations of basic data types in Swift
- In swift, you must ensure that the type is the same when you perform the basic operation, or you will get an error
- Operations can be performed between the same types
- Because there is no implicit conversion in swift
- Conversion of data types
- int type to double type: double (identifier)
- The double type is converted to int type: int (identifier), instead of rounding, leave the decimal point behind and add 0.5 if you want to round it.
let a = 10let b = 3.14// 错误写法// let c = a + b// let c = a * b// 正确写法let c = Double(a) + blet d = a + Int(b)
Swift Learning Day Two: Basic data types in Swift