当我使用 static 实现单例时,有什么可能出错的地方吗?
我正在实现一个单例类(如果这是错误的,我们就不要参与讨论)。我有一个方法来获取此类的实例,该实例是延迟初始化的:
+ (FFDataManager *)sharedDataManager {
static FFDataManager *dm = nil;
if (!dm) {
dm = [[FFDataManager alloc] init];
}
return dm;
}
与创建全局变量相比,使用 static
(在方法内部)执行此操作时是否应该注意什么?有什么可能出错的地方吗,网上所有的教程都使用全局变量。
I'm implementing a singleton class (and lets not get into the discussion if that is wrong or not). I have a method to get the instance of this class, which is lazily initialized:
+ (FFDataManager *)sharedDataManager {
static FFDataManager *dm = nil;
if (!dm) {
dm = [[FFDataManager alloc] init];
}
return dm;
}
Is there anything I should look out for when doing this using static
(inside of the method) as opposed to creating a global variable? Is there anything that can go wrong, all tutorials on the Internet use a global variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我首选的单例实现如下所示:
使用
dispatch_once
确保这也是线程安全的。当多个线程同时访问时,您的代码将分配两次。My preferred singleton implementation looks like:
Using
dispatch_once
makes sure this is also thread safe. Your code would allocate twice when accessed by multiple threads at the same time.回答原来的问题(其他人已经解决了初始化的最佳方法):
不。
区别在于可见性而不是生命周期。
static
的全局在整个应用程序中可见。可以通过在extern
语句中命名它来从其他任何地方引用它。static
的全局仅在包含的编译单元中可见(通常是单个文件,但是#import
/#include
可以改变这一点)。如果您仅在一个函数中使用全局变量,那么您所做的就是好的 - 它将可见性限制在需要的地方,同时保持执行生命周期。任何初始化程序都运行一次,就像文件级全局变量一样。
To answer the original question (others have address the best way to do initialization):
No.
The difference is over visibility and not lifetime.
static
is visible throughout the whole application. From anywhere else it can be referenced by naming it in anextern
statement.static
is visible just in the containing compilation unit (which is typically a single file, but#import
/#include
can change that).static
is a global which is only visible within that function/method.If you're using a global in just one function what you've done is good - it limits the visibility to just where it is needed while keeping execution-lifetime. Any initializer is run once, just as for file-level globals.
您创建的是一个静态局部变量。静态局部变量通过连续的方法调用保留其值。它们只能从定义它们的方法内访问。当应用程序启动时,静态局部变量将设置为 0 一次。
因此,我认为您所做的就是在每次调用
sharedDataManager
时声明一个新的静态局部变量并将其设置为nil
。我认为这没有必要,甚至没有好处。而且每次if (!dm)
检查dm
时,它都是nil
因为您将 dm 设置为 nil 之前的行。我会采用静态全局方法。
编辑:
看看 http://www.johnwordsworth。 com/2010/04/iphone-code-snippet-the-singleton-pattern/
What you create is a static local variable. Static local variables retain their value through successive method invocations. They can only be accessed from within the method which they are defined in. When apps start static local variables are set to 0 once.
So what you do in my opinion is with every call of
sharedDataManager
you declare a new static local variable and set it tonil
. I don't think that's necessary or even good. And also every timeif (!dm)
checksdm
it isnil
because you set dm to nil the line before.I'd go with the static global approach.
Edit:
Have a look at http://www.johnwordsworth.com/2010/04/iphone-code-snippet-the-singleton-pattern/