xcode 如何释放和重新初始化单例类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6502522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dealloc and re-initialize a singleton class?
提问by Kiran Kulkarni
Is it possible to dealloc a class object ?
是否可以取消分配类对象?
I have a singleton class "singleton.h" which has a single instance and we can make use of its properties in any other view controllers.
我有一个单例类“singleton.h”,它有一个实例,我们可以在任何其他视图控制器中使用它的属性。
+(singleton *)sharedMethod{
static singleton *myInstance=nil;
if(myInstance ==nil){
myInstance=[[singleton alloc] init]; myInstace.str=@"hello";
}
return myInstance;
}
what I want to know is.., Is there any way by which we can dealloc the class object in any of our viewControllers...and then again creat an instance of a new singleton class.., I tried doing so.., Xcode throws an error "cannot dealloc class object".
我想知道的是..,有什么方法可以在我们的任何 viewControllers 中释放类对象......然后再次创建一个新的单例类的实例..,我尝试这样做.., Xcode 抛出错误“无法释放类对象”。
回答by DarkDust
The whole point of a singleton is that you do not deallocate it ever. Other classes may safe a pointer to the instance, so if you want to replace it you'd get strange behavior or even crashes sometimes. So you shouldn't do it.
单例的全部意义在于你永远不会释放它。其他类可能会安全地指向实例的指针,因此如果您想替换它,有时会出现奇怪的行为甚至崩溃。所以你不应该这样做。
But it is possible, as long as you haven't overwritten the release
and retainCount
methods. But your cited error message seems to suggest you've done something along the lines of [MyClass release];
which doesn't work, of course.
但这是可能的,只要您没有覆盖release
和retainCount
方法。但是您引用的错误消息似乎表明您已经做了一些[MyClass release];
不起作用的事情,当然。
BTW, you seem to have singleton
as a class name. Please try to stick to the coding conventions used by Apple to make your life and that of other people easier. Class names should always start with an uppercase character, method names should always start with a lowercase character.
顺便说一句,你似乎有singleton
一个类名。请尽量遵守 Apple 使用的编码约定,以使您和其他人的生活更轻松。类名称应始终以大写字符开头,方法名称应始终以小写字符开头。
回答by Ofir Malachi
declare
宣布
static YOUR_CLASS *shared = nil;
static dispatch_once_t oncePredicate; //very important for reinitialize.
use instance
使用实例
+ (instancetype)shared {
dispatch_once(&oncePredicate, ^{
shared = [[self alloc] init];
});
return shared;
}
reset
重启
+ (void)reset{
@synchronized(self) {
shared = nil;
oncePredicate = 0;
}
}
you are good to go √
你很高兴 √
回答by Nitish
It is very considerable if you don't dealloc the singletonclass because it is not advised so till the complete excution of your application. See thisfor more imformation. To reinitialize your singletonclass you need to do the same as you did for the first time.
如果您不释放单例类,这是非常可观的,因为在您的应用程序完全执行之前不建议这样做。有关更多信息,请参阅此内容。要重新初始化您的单例类,您需要像第一次那样做。
回答by haifacarina
This solution helped me out. Create a separate method for initializing the class.
这个解决方案帮助了我。创建一个单独的方法来初始化类。
@implementation SomeManager
static id sharedManager = nil;
+ (void)initialize {
if (self == [SomeManager class]) {
sharedManager = [[self alloc] init];
}
}
+ (id)sharedManager {
return sharedManager;
}
@end