From Objective-C to Swift Singleton Mode

Source: Internet
Author: User

From Objective-C to Swift Singleton Mode
The Singleton mode is often used in Objective-C. The most common is: [UIApplication sharedApplication]. delegate the sharedApplication here is a way to return a singleton. The so-called Singleton mode is only such an object of this class in the entire APP or software. You can save some global settings and other data to achieve the effect that the single-instance object changes all settings of the entire app are updated. Ignore the OC code and directly access the Swift code. Implementation of the first Singleton1 mode: copy the code class Singleton1 {class var sharedInstance: Singleton1 {return _ SharedInstance} private let _ SharedInstance = Singleton1 () var s1 = Singleton1 () copying Code is the first and simplest implementation of the singleton mode. Declare a global private constant outside the class to be implemented by a Singleton, private let _ SharedInstance = Singleton1 (). Then, declare a class property in the computed property of the singleton class and return the instances of the singleton class outside the class. This is a simple but effective method! Implementation of the second Singleton2 example: copy the code class Singleton2 {class var sharedInstance: Singleton2 {struct Instance {static let instance: Singleton2 = Singleton2 ()} return Instance. instance} var s2 = Singleton2 () Copying code this implementation uses the embedded type. A struct is defined in computd property. In the entire struct, a static (class uses the class keyword to declare static methods or attributes) constant attribute initializes a singleton instance. Return the total instance of the struct In the computed property of the singleton class. Example 3: copy the code class Singleton3 {class var sharedInstance: Singleton3 {struct Instance {static var onceToken: dispatch_once_t = 0 static var instance: Singleton3? = Nil} dispatch_once (& Instance. onceToken) {Instance. instance = Singleton3 ()} return Instance. instance!} Var s3 = Singleton3 () Copying code this implementation uses something similar to GCD in OC. This Singleton is thread-safe. The implementation method is similar to the second Singleton, but the thread lock is added to ensure safe calls in the case of multithreading. The key to implementing the singleton mode is that the constructor of the singleton class cannot be accessed at will. All of our implementations can initialize the instance at will. The effect achieved is that the effect of a single instance can be achieved according to the agreed call method. In the above implementation. The first type defines a private constant instance. In addition to the agreed call method, other classes cannot access this private constant. The second and third types are similar. They both define a nested type in computed property. In this way, the nested type in the computed property cannot be accessed in other class objects. The second and three differences are: the second is to initialize the single-instance directly in the struct, and the third is to implement the thread security mechanism in the computed property.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.