假设有一个AccountManager类,想要在整个应用中访问该类的共享实例,可以这么写
+(AccountManager *)shareManager {
//
static AccountManager * sharedAccountManagerInstance = nil;
static dispatch_once_t predicate; dispatch_once(&predicate,^{
sharedAccountManagerInstance = [self alloc] init];
});
return shareAccountManagerInstance ;
}
在任何地方想要共享实例 只需要
AccountManager * accountManager = [AccountManager shareManager];
时间: 2024-11-05 19:41:49