单元测试期间静态函数初始化中奇怪的单例初始化
我的单例类中有以下代码
static MySingleton *gManager;
+(void)initialize
{
if(self == [MySingleton class])
{
gManager = [[MySingleton alloc] initWithServices:[[MyServices alloc] init]];
}
}
+(MySingleton *)sharedInstance
{
return (gManager);
}
不幸的是,在单元测试期间我看到 gManager 是 SenTestCaseRun 类型的实例。我似乎无法弄清楚为什么? 所以像这样的电话 [[MySingleton 共享实例] myFunction];
导致错误:myFunction 是未知选择器,尽管它存在于 MySingleton 类中。
它是 SenTestCaseRun 类型,因为我使用 NSStringFromClass 函数进行检查。
有什么指点吗?已经为此敲了我的头 3-4 个小时了:(。
I have a following code in my singleton class
static MySingleton *gManager;
+(void)initialize
{
if(self == [MySingleton class])
{
gManager = [[MySingleton alloc] initWithServices:[[MyServices alloc] init]];
}
}
+(MySingleton *)sharedInstance
{
return (gManager);
}
Unfortunately, during the unit tests I see that gManager is an instance of type SenTestCaseRun. I cant seem to figure out why?
So a call like
[[MySingleton sharedInstance] myFunction];
results in an error that myFunction is an unknown selector although it exists in the MySingleton class.
It is of type SenTestCaseRun because I checked using NSStringFromClass function.
Any pointers? Already banged my head for 3-4 hours on this :(.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
+initialize
中放置一个断点以确保该变量设置正确。如果这不能解释它,请使用Put a breakpoint in
+initialize
to make sure this variable is set correctly. If that doesn't explain it, use a watchpoint on it to see who's modifying it.最好将初始化代码放在共享实例方法中
,也在代码中将对象与永远不会为真的类进行比较,而不是将 [self class] 与 [MySingleton class] 进行比较。
it may be better to just put the initialization code inside the shared instance method
also in your code you are comparing an object to a class which will never be true instead of comparing [self class] to [MySingleton class].