|
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...
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 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)
As Paul say "Everyone has something to learn, Everyone has something to teach" |
|
You might also be interested in VisualStudio for Mac. |
|
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
The App struct also can have a https://developer.apple.com/documentation/swiftui/app/main()/ |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.