我已经使用常规模式实现了一个单例对象。我的问题是:是否可以将此对象设置回 nil,以便在稍后调用 [MySingleton sharedInstance] 时重新初始化该对象?
// Get the shared instance and create it if necessary.
+ (MySingleton *)sharedInstance {
static dispatch_once_t pred;
static MySingleton *shared = nil;
dispatch_once(&pred, ^{
shared = [[MySingleton alloc] init];
});
return shared;
}
// We can still have a regular init method, that will get called the first time the Singleton is used.
- (id)init
{
self = [super init];
if (self) {
// Work your initialising magic here as you normally would
}
return self;
}
我的猜测是
MySingleton *shared = [MySingleton sharedInstance];
shared = nil;
仅将本地指针设置shared
为nil
. 毕竟,shared
被声明为static
.