WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Best practice for implementing helper functions in Swift: Static vs. Singleton vs. Instance Method?

Forums > Swift

Hey everyone,

I’m currently grappling with coding fundamentals, particularly what I would refer to as 'helper classes.'

For instance, when I need a function that makes an API call and returns some data, I would create a class called APIService to house that function, fetchData(args) -> 'data.'

  1. Should I define APIService as a class or a struct?
  2. How should I implement fetchData:
    • Should I define fetchData as a static function in APIService and call it with APIService.fetchdata?
    • Should I make APIService a Singleton and call APIService.shared.fetchdata?
    • Should I define it as a 'normal' method of APIService, and when calling it, create a new instance of APIService and call it like newAPIService.fetchdata?

It would be greatly appreciated if someone could help me sort this out.

Thanks a lot! Harry


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.