ios单例
方式一、单例
+ (SocketUtils *)getInstance;
static SocketUtils *instance = nil;
+ (SocketUtils *)getInstance {
if (nil == instance) {
//Socket
instance = [[SocketUtils alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:WEBSOCKET_SERVER]]];
}
return instance;
}
方式二、单例
用dispatch_once函数:
+ (SVProgressHUD*)sharedView {
static dispatch_once_t once;
static SVProgressHUD *sharedView;
dispatch_once(&once, ^ {
sharedView = [[SVProgressHUD alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
});
return sharedView;
}