I have not found myself using a singleton in a while because I get all the functionality of one from the static class. Are there any differences between the two that I should be aware of?
Singleton Example:
class SomeManager {
static let sharedInstance = SomeManager()
var user: User!
init(user: User) {
self.user = user
}
}
With usage:
SomeManager.sharedInstance.user
Static Class Example:
class SomeManager {
static var user: User!
init(user: User) {
SomeManager.user = user
}
}
With usage:
SomeManager.user
I know the obvious difference is that one is an entire class being made static, and the other is declaring specific parts (that are desired to be from a single instance) static. Is there any reason to use one over the other.
I have one setup for my Network right now where I have a singleton class of network calls that are accessed through a bunch of static methods in a class that does nothing but contain the Network singleton and its static methods. I find this to have the appropriate scope for my scenario, but have not done enough research to guarantee the quality of the method.
Example:
class NetworkCalls {
static let network = Network()
static func getToken(completion: () -> Void) {
network.apiGetToken() {
completion()
}
}
}
With usage:
NetworkCalls.getToken() {
print("Network Call Completed")
}
It works fine, so I am only looking for matters of efficiency, things to think about, and differences between this and alternative methods. Any tips/advice would be highly appreciated!