I want to create a class where all utility methods will be kept and these methods will be used throughout the app.
Problem:1
Is it good to create a singleton class and keep all necessary methods there or should I create a class where all function will be static.
Problem:2
What is main difference between above two approaches in swift ?
Problem:3
How it will impact performance and memory in iOS?

- 2,653
- 1
- 25
- 36
3 Answers
Sure this sounds confusing and can be debated. However, from the best practices i can put some suggestions.
Singleton is usually used to create a resource intensive and one timer initialisation for instance: a database connector, login handler and such.
Utility class are classes that only have static functions and variables. It should not deal with async task and expensive resource handling like opening a database connector.
In your case, if the utility is doing some resource intensive process its better to wrap in a singleton. If not, then Static functions in a class is better in my opinion. This is so also because, Swift will dispatch all static functions in a class using static dispatch. Whereas this cannot be true in Singleton although Swift likes to optimizes.
Static Dispatch are 4 times faster than Dynamic Dispatch as far as Objective-C runtime is used. This is true for swift too. However, it only takes 4 nano seconds to dispatch Dynamiclly.
I hope this makes you clear.

- 1,545
- 12
- 20
-
3To be clear, Invoking an instance method does not necessarily imply dynamic dispatch is used. Instance methods on a singleton can have the exact same performance if they can be statically dispatched (such as in cases where the method is marked `final`, or if whole module optimization is enabled) – Alexander Dec 24 '16 at 06:29
-
14Why should you not have async tasks in a utility class? What kind of problems does this cause? – Nathan Dudley Mar 28 '17 at 03:16
-
1I have the same question why not use async methods in Utility class? – MikeG Oct 16 '17 at 00:57
-
@NathanDudley ever get your answer? – Dzondzula Nov 07 '22 at 00:18
-
1@Dzondzula there is no reason to prefer a singleton over a utility class solely due to asynchrony. I believe a singleton should only be preferred when trying to maintain state across the duration of the app lifecycle. Especially with the “new” async/await APIs, I would prefer to use a utility class with static methods. – Nathan Dudley Nov 08 '22 at 04:35
-
@NathanDudley thank you, 5 years ago but still an active question. – Dzondzula Nov 08 '22 at 16:24
Lets say we have two classes.
Class with all static methods and static variables This way you can access the class methods without instantiating an object. Also if some small data handles are required they can be stored in static variables. All the threads accessing this class would not end up creating duplicate instances of data variables.
Singleton Class This class will have a private init method and share a single instance through a static instance. All the threads accessing the instance would not end up creating duplicate instances of data variables.
So technically both may sound very similar for your scenario of a Utility class and it might get confusing to make a decision, you may use following use cases to make a decision.
Does this class primarily defines logical implementations? If the primary purpose of class methods is to provide logical calculations/manipulations or operations that do not require storing data in variables besides temporary handles then you should always opt for a Static Class. For e.g. Utility class that you have mentioned here. Utility methods like resizing an image, reading a file, parsing a data structures are are best done using Static methods.
If your class requires storing important information in multiple variables, limiting resource access (e.g. limiting number of simultaneous transactions on database or limiting number of simultaneous network calls), where it frequently allocates and deallocates resources (manages the memory at runtime) then its best to use a Singleton class (For e.g. class for accessing db structures, managing network resources, etc..)

- 862
- 5
- 10
-
1Does it impact app performance by creating too many static function ? – Sunil Sharma Jun 14 '16 at 10:29
-
Technically more code you have, the bigger will be size of your executable and will impact your app performance. But this happens when you are writing huge solutions. In such cases have to focus on managing execution and runtime memory by different architectural alternatives (pluggable modules, service, shared library, etc...). But most of the mobile applications do not fall into this criteria so its not a big worry in most cases. Also the performance impact of Singleton vs static method in utility classes won't be any different. – chejaras Jun 15 '16 at 07:28
-
1Do you see your answer changing somehow if we used struct instead of a class (with all static funcs and properties)? – Raul Huerta Nov 02 '16 at 17:30
-
Hi Raul. Struct and class are essentially same in C++. I am not sure if your question was in context to switf – chejaras Dec 25 '16 at 06:36
Major difference between static
and singleton
is that Singleton
can implemented Protocols
and derive from some base classes. In case of Singleton
, class can be instantiated but only once. Static
functions can be used directly without instantiation.
So if you want to create class for utility methods it should be a class with static utility functions and not singleton. Both static and singleton class can be implemented thread safe.

- 3,896
- 21
- 35
-
A protocol can also define static methods, both in Swift and in Obj-C. Also static methods can be inherited, as the question was not about static functions but about a class with static members. – Mecki Jun 10 '20 at 15:10