I've got the following Singleton class:
class Singleton {
static let sharedInstance = Singleton()
}
I can find very little online about how to use the numerous swift implementations of the Singleton pattern. I have used it before in Objective-C on a previous application but to me it seemed much more straight forward.
For instance, if I wanted to create an array of custom objects that could be used anywhere in the application, how would I declare it, and how would I implement it. In my objective-C Singleton class, I create global variables in the class file, and then implement it like so:
singletonClass *mySingleton = [singletonClass sharedsingletonClass];
mySingleton.whatever = "blaaaah"
I appreciate the help! Also I'm new around here and new to Swift.
There is a lot of info available on singletons in Swift. Have you come across this article with your Google prowess? http://krakendev.io/blog/the-right-way-to-write-a-singleton
But to answer your question, you can simply define anything you'd like to use normally.
Agree with Andrew Sowers. Just remember that you must also declare a private initializer like this:
Without this private init(), other objects could create their own instance:
Now there are two instances, Singleton.sharedInstance and mySingleton - no longer a singleton! I discovered this via a nasty bug where multiple "singletons" were firing timers and wreaking havoc.