To the extent that your question is "how to run code when your app launches" rather than how to distribute methods among your files and objects, your answer is superficially addressed in the topic “How to run code when your app launches” in the online book "SwiftUI by Example":
https://www.hackingwithswift.com/quick-start/swiftui
Because it is organized topically, you might prefer reading "SwiftUI by Example" instead of working through the "100 Days of Swift" examples. You can also buy it as a PDF.
You can also declare in your App
struct instance properties (e.g., vm
and skale
) that you can pass to other objects as dependencies and/or inject in the SwiftUI Environment and static properties (e.g., audioPlayer
) that any code can access globally, as in this example:
@main
struct MainApp: App {
var vm: ViewModel
var skale: Skale
static var audioPlayer = AudioPlayer()
init() {
self.vm = ViewModel()
self.skale = Skale(vmSkale: vm)
}
var body: some Scene {
WindowGroup {
ContentView().environmentObject(vm)
}
}
}
The App struct also can have a main()
method that I have never seen used in a SwiftUI app, but which I presume runs immediately after the init()
method. It might be useful to initialize properties in a way that cannot be done in the init()
because you need to reference self
. If anyone has used main()
, I hope you contribute to this discussion!
https://developer.apple.com/documentation/swiftui/app/main()/