I’ve read several threads about the differences between Singletons and Classes in various languages, but none (that I can find) specific to Objective-C.
They seem very similar to me.
I’m new to objective-c – so what are the differences, or usage cases between Singletons and Classes?
You are programming in Objective-C, an Object-oriented programming language (OOP). So, to know what a class is, you must first understand OOP concepts.
I wont go into why OOP languages was developed as opposed to procedural languages. You can google “procedural vs objective oriented advantages”.
Anyway,
Object Oriented Programming languages (OOP) provide mechanisms/devices/structures that helps you implement OOP principles. 3 main principles are (1)Encapsulation (2)Inheritance (3) Polymorphism. Again google to find out what these are.
Now a Class: would be one of those mechanisms/devices/structures that implements the 3 OOP principles. It is a blueprint/design/structure of what an object should be.
Typically (but not always) a class would have (1) variables declared within it (strings, int, arrays, whatever) and (2) Methods/functions that perform a task on those declared variables within it. When you instantiate/allocate/create-that-class-in-memory, that class becomes “alive”. The blueprints now become an object (an object that implements the OOP principles you are supposed to google).
Now a Singleton is a design pattern. If you’re a coming from a procedural language environment, there are many programming design patterns you may need to catch up on. A Singleton is a pattern/a-particular-way-you-design-someting. So when you say a singleton class, it is a particular way you design that class. And what is that particular design? Well, usually when you instantiate/allocate/create-a-class-in-memory, you can do it many times, there by creating multiple instanced objects of that class. If you apply a singleton design pattern to a class however, you ensure that only one instance (and ONLY one) can be created.
In Objective-C if i do something like this:
In short.. you really cant compare the two.
As for the usage of singletons.. I would say Loggers. I think there are already answers around stackoverflow about uses of singletons. Look around a bit.