TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Creating functions best practice

Forums > Swift

As a developer I try to keep my code as neat and tidy as possible and to not repeat myself if necessary. I love the idea of seperation of concearns.

I notice when I watch a lot of the videos that functions are created in the struct of the content view. My issue is, that I want all my logic some place else. I have created extensions as viewmodels for my views, but even that has code I would like to pull out because it can be used in different places in my code. Especially when I am dealing with CoreData.

My question is this. I have functions that syncronize data using REST calls, as well as other things I like to keep together. I have created a SWIFT file where I put all these functions in. This works great, but what should be my best practice? I personally like the idea of creating a class object and that object contains all my referenced code for that type.

Example. I have many functions with dealing with users. I sync my users with a REST call. I update my users in CoreData, I authenticate my users, I get the count of the number of users, etc.... I can do all this without a class object, but I like having all of that available within a class. Because it is in a class, I can have private functions that are not available outside the class.

When I watch the videos there isn't a clean object model and I assume this is for ease of learning or is this a standard within swift. One of your videos says to stay away from class and use struct.

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.