Created
June 3, 2015 07:47
-
-
Save ixqbar/712aa2439e64b3212172 to your computer and use it in GitHub Desktop.
swift单例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SwiftSingleton { | |
class var shared: SwiftSingleton { | |
return Inner.instance | |
} | |
struct Inner { | |
static let instance: SwiftSingleton = SwiftSingleton() | |
} | |
} | |
class SwiftSingleton { | |
class var shared: SwiftSingleton { | |
dispatch_once(&Inner.token) { | |
Inner.instance = SwiftSingleton() | |
} | |
return Inner.instance! | |
} | |
struct Inner { | |
static var instance: SwiftSingleton? | |
static var token: dispatch_once_t = 0 | |
} | |
} |
Author
ixqbar
commented
Nov 19, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment