A singleton pattern is often used in objective-c. The most common are:
[UIApplication sharedapplication]. Delegate
The sharedapplication here is a way to return the singleton. The so-called Singleton pattern is the only object of this class in the entire app, or software. You can save some global settings and other data to achieve a single object changes the entire app's settings are updated effect.
Ignore the OC code and go directly to the SWIFT code.
Implementation of the first singleton pattern:
class singleton1{ classvar sharedinstance:singleton1 { return _sharedinstance }}private let _sharedinstance = Singleton1 ()var s1 = Singleton1 ()
This is the first, simplest implementation of the singleton pattern. Declare a global private constant outside the class that requires the singleton implementation, private let_sharedinstance = Singleton1 (). After that, a class attribute is declared in the computed property of the Singleton class, and an instance of the Singleton class outside of the class is returned. Very simple, but effective method!
The second type of singleton implementation:
class singleton2{ classvar sharedinstance:singleton2{ struct instance{ static Let Instance:singleton2 = Singleton2 () } return instance.instance }}var s2 = Singleton2 ()
This implementation uses an inline type. A struct is defined in the Computd property. And in the whole structure declare a static (class inside the class keyword to declare a static method or property) constant property Initializes a singleton instance. and returns an instance of the struct in the computed property of the Singleton class.
The third type of single case:
classsingleton3{class varsharedinstance:singleton3{structInstance {Static varoncetoken:dispatch_once_t =0 Static varInstance:singleton3? =Nil} dispatch_once (&Instance.oncetoken) {instance.instance=Singleton3 ()}returninstance.instance! }}varS3 = Singleton3 ()
This implementation uses something similar to GCD in OC. This single case is thread-safe. It is implemented in a way similar to the second single case, with the addition of a thread lock, which guarantees secure invocation in multithreaded situations.
The key to implementing the singleton pattern is that the constructors of the singleton classes cannot be accessed arbitrarily. All of our implementations can actually initialize the instance arbitrarily. It can achieve the effect is in accordance with the agreed call method can achieve the effect of a single case. In the above implementation. The first defines a private constant instance, and other classes are not accessible to this private constant outside of the agreed calling method. The second and third comparisons are similar in that a nested type is defined in the computed property. In this way, the inline type in this computed property cannot be accessed in other class objects. The second to third point differs in that the second is to initialize the singleton instance directly in the struct, and the third is to implement the thread safety mechanism in the computed property.
From objective-c to Swift single-case mode