1、OC写法
+ (instanceType)sharedSoundTools {
static EFSoundTools *instance = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
2、仿OC写法
class EFSoundTools: NSObject {
static var instance: EFSoundTools?
static var oneToken: dispatch_once_t = 0
class func sharedSoundTools() -> EFSoundTools {
dispatch_once(&oneToken) { () -> Void in
instance = EFSoundTools()
}
return instance!
}
}
3、swift写法
static let sharedSoundTools = EFSoundTools()
4、需要自定义构造函数写法
static let sharedTools: EFNetworkTools = {
let baseURL = NSURL(string: "https://api.weibo.com/")
let tools = EFNetworkTools(baseURL: baseURL)
return tools
}()
网友评论