Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run function when "viewDidLoad" is called while using SwiftUI

Tags:

swift

swiftui

I'm trying to run a function when the app finish loading up, like viewDidLoad, but I'm using SwiftUI now and I have no viewDidLoad. How can I do this now?

var body: some View {
    NavigationView {
        Form {
            Section {
                self.exampleFunction()

                Text(" ......  ")
            }
        }
    }
}

I want to take some information from that function and present it in the text. But the way I'm doing it is wrong. It's not building.

like image 860
vToM Avatar asked Jul 18 '19 17:07

vToM


Video Answer


1 Answers

You can use .onAppear { ... } to execute arbitrary code when a view appears:

var body: some View {
        NavigationView {
            Form {
                Section {
                    Text(" ......  ")
                }.onAppear { self.exampleFunction() }
like image 200
Bradley Hilton Avatar answered Oct 14 '22 17:10

Bradley Hilton