SwiftUI - View 生命周期

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//
// CalendarSelectView.swift
// cdiary
//
// Created by hanks on 2021/8/27.
//

import SwiftUI

struct ContentView: View {


init() {
// 1
print("init code run ......")
}


var name : String{
// 3
print("get field code run ......")
return "Hanks"
}

var body: some View {
// 2
print("get body view code run ......")
return Text("Hello,\(name) World!")
.onAppear(perform: initData)
}

private func initData(){
// 4
print("onAppear code run ......")
}
}
1
2
3
4
5
6
7
init code run ......
init code run ......
init code run ......
App is active
get body view code run ......
get field code run ......
onAppear code run ......

文章来自: http://hanks-zyh.github.io/