Contact: Shihu QQ: 1224614774 Nickname: Om Mani Padme Coax
QQ group: 807236138 Group name: iOS technology exchange learning group
QQ group: 713799633 Group name: iOS technology exchange learning group-2
reference:
1. Data type
Data types include constants, variables, expressions
Constant: It is the amount whose value will not change during the running of the program. Use the keyword let to declare let name = "puxin"
Variable: It is the amount that can change the value during the running of the program. Use the keyword var to declare var age = 12
Expression definition: let index = % 2
type annotation
Multiple variables or constants can be declared on one line, separated by commas
Constant and variable names cannot contain whitespace characters, mathematical symbols, arrows, reserved (or invalid) Unicode code points, hyphens, and tabs. Nor can it start with a number, although numbers can be used almost anywhere else in the name
CamelCase: lowercase the first letter, capitalize the first letter of other words
Type declaration: newly proposed by swift 4, plus the type declaration tells the compiler the value type stored in the constant and variable, and at the same time initializes, that is, assigns an initial value to the constant and variable
Definition: let name: String = ""
var age: Int = 1
Print output function print()
Add a comment to code:
Comments are an important part of the code, which can greatly improve the readability of the code. Generally, about one-third of the high-quality code is comments
Format: // /** */ ///
Statement separator:
After writing a line, there is no need to add a semicolon. If you enter multiple statements in one line, you can use a comma as a separator for a statement
Example: var a_b = “a and b” , aperson = “one person”
Integer and Float
There are two main types of numbers namely Integer and Float
Integer types are divided into signed types and unsigned types
Signed types: Int8 Int16 Int32 Int64
Unsigned type: UInt8 UInt16 UInt32 UInt64
Out-of-bounds assignment:
Pay attention to the range of values when assigning values to different types of integers, and do not assign values beyond the bounds.
通过整型的 min 和max 属性获取不同整型的最小值和最大值
print("Int8.max = \(Int8.min) Int16.min = \(Int16.min) Int32.min = \(Int32.min) \(Int64.min) INT8_MIN = \(INT8_MIN)")
print("Int8.max = \(Int8.max) Int16.max = \(Int16.max) Int32.max = \(Int32.max) Int64.max = \(Int64.max) INT8_MAX = \(INT8_MAX)")
打印结果:
Int8.max = -128 Int16.min = -32768 Int32.min = -2147483648 Int64.min = -9223372036854775808 INT8_MIN = -128
Int8.max = 127 Int16.max = 32767 Int32.max = 2147483647 Int64.max = 9223372036854775807 INT8_MAX = 127
Floating-point mixed operations
The floating-point type swift provides two types, the floating-point type float and the double-precision floating-point type double used for low precision
Boolean value
Boolean values have only two values "true" and "false", which are represented by the keywords true and false respectively.
Example: var daylight: Bool = true
Boolean-related logical operators, also called Boolean operators
tuple type
tuple definition:
The tuple type is a composite type composed of one or more data types, each of which can be any type and does not require the same
Example: let http404Error = (404, “Not Found”)
Usage scenario: Indicate the length and width of a rectangle, three-dimensional coordinates, etc.
read tuple
There are three ways to read:
第一种:将元组的值赋值给另一个元组
例子:
let http404Error = (404, "Not Found")
let (code, description) = http404Error
print("code=== \(code)") // code=== 404
print(description) // Not Found
第二种:通过下标访问元组的特定元素,下标是从 0 开始分别自左向右表示不同的元素
例子:
print(http404Error.0) // 404
print(http404Error.1) // Not Found
第三种:在定义元组的时候给每一个元素命名,通过这些元素的名字获取元素的值
例子:
let http404Error = (code: 404, description: "Not Found")
print(http404Error.code) // 404
print(http404Error.description) // Not Found
Replenish:
Advantages - can be used as the return value of the function, greatly increasing the readability of the return value,
Disadvantages - tuples are not suitable for creating complex data structures, especially long-term data types. In this case, structures or classes should be used to describe data structures
Optional
1. Swift has a special data type, usually called optional optional, which is an important embodiment of swift's built-in security features
2. The optional type is used to indicate that a variable or constant may have a value or may not have a value. Use nil to indicate empty. Note (the integer 0 is not nil, and the empty string "" is not nil)
3 .nil can be understood as there is no data in the storage space corresponding to the variable or constant
Example difference sub:
Describe with a string:
var studentInObjc: (name : String, idNUmber: String, age: Int, reward: String)
studentInObjc = ("Tom", "13261089216", 20, "")
// 描述中的reward 没有用可选类型,赋值中的""虽然是空字符串也是一个取值,如果有文档规定空字符表示特定的情况,否则其他程序员很难理解这个代码
Described with a string optional
var studentInSwift: (name : String, idNUmber: String, age: Int, reward: String?)
studentInSwift = ("Tom", "13261089216", 20, nil)
// 描述中的reward 用可选类型String?,有值为字符串,无值就显示 nil
Define an optional type:
声明一个可选类型的常量或者变量的格式
let cName :Type? = value
var cName :Type? = value
声明一个变量reward 其是字符串可选类型,可以赋值,也可以不赋值,默认为 nil
var reward: String?
Understanding optionals:
The optional type can be imagined as a box. The optional type can represent an actual value or nil. This box can contain an actual value or be empty, but whether it contains the actual value or not, the box is always existing
There are three ways to open this optional box and drive the data in it: forced unpacking, optional type binding, nil aggregation operation
第一种方式 — 强制拆包 : 用 ! 强制拆包
例子:
var reward: String?
reward = “wo shi lao shi ”
var bonus: String = “value”
bonus = reward!
print(“\(bonus)”)
注意: 强制拆包带来的问题会导致系统崩溃,被解包属性必须有数据才可以,
防止崩溃 应该这样
if reward != nil {
bonus = reward!
}
The second way — optional type binding:
Optional binding is to assign the optional type to a constant in the conditional judgment statement of if or while. If the optional type contains a value, the Boolean value of this assignment statement is true, and the constant gets optional The value contained in the type, if the optional type is equal to nil, the Boolean value of this assignment statement is false.
格式如下:
var optionalName: String?
optionalName = "可选类型绑定"
if let constantName = optionalName {
print("可选类型绑定 == \(constantName)")
} else {
print("可选类型绑定 == kong")
}
The third way - nil aggregation operation: use ?? as nil aggregation operator
The method of taking values from optional types is called nil aggregation operation. It is the easiest way to unpack optional types.
Regardless of whether there is a value in the optional type, it can be unpacked through the nil aggregation operation.
If there is a value, it will be taken out. If the optional type is nil, the default value will be used instead.
格式如下:
var hobby: String?
var membership: String?
// membership = "membership"
let getHobby = hobby ?? "No hobby"
let getMembership = membership ?? "No membership"
hobby = nil
membership = nil
print("nil 聚合运算 =getHobby == \(getHobby)")
print("nil 聚合运算=getMembership == \(getMembership)")
打印结果:
nil 聚合运算 =getHobby == No hobby
nil 聚合运算=getMembership == No membership
Points to note when using the nil aggregation operator:
1. The type of the default value must be consistent with the unpacked type of the optional type,
2. Objects performing nil aggregation operators must be optional
Second, the operator
Please see the next text...
Thanks!!!
This account mainly shares my various experiences and insights in the process of growing up, including technical summaries, iOS, Swift and Mac related technical articles, tool resources, participation in technical discussions, sorting out development skills, and making learning a pleasure!