General wording:
Class Swiftsingleton {class var Shared:swiftsingleton {if! inner.instance {inner.instance = Swiftsingleton ()} returninner.instance! } structinner {Staticvar Instance:swiftsingleton? }
}
However, multiple threads in the system are executing, in which case multiple instances are created, and there is no guarantee of uniqueness of the instance. can also be modified into another way:
|
class Swiftsingleton { classvar Shared:swiftsingleton { & Nbsp; returninner.instance } struct Inner { static Let Instance:swiftsingleton = Swiftsingleton () }
}
|
The Swiftsingleton is not created when you first access inner.instance, but it does not create multiple objects, but it is not a good practice to return objects that you want to modify or retrieve again.
so you can use GCD to achieve:
Class Swiftsingleton {Classvar Shared:swiftsingleton {dispatch_once (&inner.token) {inner.i Nstance = Swiftsingleton ()} return inner.instance! } struct Inner {static var Instance:swiftsingleton? static var token:dispatch_once_t = 0}}
IOS Swift Singleton mode