Skip to content

Instantly share code, notes, and snippets.

@ixqbar
Created June 3, 2015 07:47
Show Gist options
  • Save ixqbar/712aa2439e64b3212172 to your computer and use it in GitHub Desktop.
Save ixqbar/712aa2439e64b3212172 to your computer and use it in GitHub Desktop.
swift单例
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
}
}
@ixqbar
Copy link
Author

ixqbar commented Nov 19, 2015

+ (id)shareInstance
{
    static CRSA *_crsa = nil;   //CRSA is class name
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _crsa = [[self alloc] init];
    });
    return _crsa;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment