2015-04-24 131 views
-1

我已经创建了单例对象,在一个时间点上必须释放单例对象。如何在非ARC和ARC中释放单例对象?如何在iOS中发布Singleton对象?

+0

http://www.galloway.me.uk/tutorials/singleton-classes/ –

+0

看看在非圆弧代码我提供 –

+0

链接 - (单向无效)发布{// 绝不会透露 } 我们如何使用这种方法? –

回答

0

如果你把单个实例作为类的一个全局变量,例如:

static MyClass *_instance = nil; 

而不是sharedInstance类方法中是static地方,那么你可以创建这样的破坏方法:

+ (void)destroyInstance 
{ 
    _instance = nil; 
} 

但是我可以看到的一个问题是使用dispatch_once_t这是常用的确保原子初始化;我认为你需要避免在这种情况下使用它,因为它不可能重置它。如果你再也不打算再次致电sharedInstance,这可能不是问题。

0
@interface MySingleton : NSObject 
static dispatch_once_t predicate; 
static MySingleton *sharedSingletonInstance = nil; 

@implementation MySingleton 

+ (MySingleton *)ShareInstance { 
    dispatch_once(&predicate, ^{ 
     sharedSingletonInstance = [[self alloc] init]; 
    }); 
    return sharedSingletonInstance; 
} 

+ (void)destroyMySingletonInstance { 
    sharedSingletonInstance = nil; 
    predicate = 0; 
} 
- (void)dealloc { 
    NSLog(@"------"); 
} 

// TODO 
... 
@end