UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Beginner Question

Forums > SwiftUI

Sorry to be so dumb but I am struggling to understand where to put my application logic in my SwiftUI application?

I am a 20+ years experience programmer but I am obviously missing something. For my app, I'd like to do some basic setup. Eg: Load some data structures from a db and store them in my model objects, connect to remote server to start listening for messages. Basically "non-ui" code. I can define my methods etc in classes/structs as I would expect but I can't seem to execute them in sensible way as everything seems to hang of a "view"

Consider this flow...

print("Hello world!")
var x = myclass()
x.DoSomeStuff()
x.LoadSomeStuff()

var y = x.MyProperty

Print(y)

Where the heck do I put code like this?

Thanks!

   

Thanks for the response. No not running this code in playgrounds. I'm re-writing a Windows app for MacOS and using Xcode. The code above was simply to illustrate my point, that I want to execute arbitrary code to execute application logic that doesn't explicitly belong in the view yet. I am fairly sure that we aren't supposed to put our business logic in the view.

I will dig into the 100 days of code to try and see where I'm going wrong but a pointer to which day you believe would will help answer my question would be appreciated.

To be clear..I am a developer with many years experience in Java/C++/C# and I do this for a living, but I am new to the Mac and Swift/SwiftUI. so respectfully please don't patronise, this simply leaves a bad taste in the mouth and I suspect is not the point of this otherwise helpful community.

   

I think @Obelix is say that without more information on what you trying to do will depand on where you place the code. If you work though the course you will see different methods, and some time they are in the View sometime there are in the class , viewModel etc. I am sure that with all your experence that someone ask a vague question in one of your know laugauges what would your response be!

The 100 days of SwiftUI is designed to build on previous tutorials so jump into one might get you lost. However some will be quick and some you can read over but would do it in order. (I have done this at least three times as it updated every year)

@Obelix and I answer a lot of questions and are very willing to help where we can.

As Paul say "Everyone has something to learn, Everyone has something to teach"

   

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()/

   

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.