NSMutableDictionary 的 setObject 函数崩溃
这是我的代码:
NSLog(@"dictionaryPlayers=%@,%d",[dictionaryPlayers description],dictionaryPlayers.count);
[dictionaryPlayers setObject:@"test" forKey:@"test2"];
dictionaryPlayers 在此类的 init 函数中初始化:
-(id)init{
...
dictionaryPlayers = [[NSMutableDictionary dictionaryWithCapacity:10]retain];
...
}
程序崩溃:
Thread 1:Program received signal: "SIGABRT".
在控制台中:
2011-12-27 17:01:21.744 [25454:207] dictionaryPlayers={
},0
2011-12-27 17:01:21.745 [25454:207] -[__NSCFConstantString tick]: unrecognized selector sent to instance 0x199bcc
通过 NSLog 输出,我认为dictionaryPlayers 初始化得很好。所以不知道为什么会崩溃...
Here is my code:
NSLog(@"dictionaryPlayers=%@,%d",[dictionaryPlayers description],dictionaryPlayers.count);
[dictionaryPlayers setObject:@"test" forKey:@"test2"];
dictionaryPlayers is inited in this class's init function:
-(id)init{
...
dictionaryPlayers = [[NSMutableDictionary dictionaryWithCapacity:10]retain];
...
}
The program crashed:
Thread 1:Program received signal: "SIGABRT".
And in console:
2011-12-27 17:01:21.744 [25454:207] dictionaryPlayers={
},0
2011-12-27 17:01:21.745 [25454:207] -[__NSCFConstantString tick]: unrecognized selector sent to instance 0x199bcc
With the NSLog outputs, i think dictionaryPlayers is well inited. So I don't know why crashed...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我
从 init() 中删除,并
在日志语句上方添加。还是崩溃了....
然后我删除了
所以只剩下两行:
它没有崩溃。所以,看来 setValue 行确实是问题所在。
I removed
from init(), and added
above my log statement. Still crash....
Then I removed
So there's only 2 lines left:
It didn't crash. So, it seems the setValue line really is the problem.
您不需要在 init 语句中对对象调用保留。也只是为了咯咯地笑,尝试:
而不是在
日志语句上方执行此操作(将其从 init 中取出)。
如果这有效,请将日志放入您的 init 方法中,并确保在将 KV 添加到字典的方法之前调用该方法,
我无法重现此行为。这是我的代码:
ViewController.h:
ViewController.m:
没有问题。如果你这样做的话,就不会有问题。以下是我进一步排除故障所采取的步骤:
You don't need to call retain on the object in your init statement. Also just for giggles try:
instead of
And do this right above your log statement (take it out of the init).
If this works, put a log in your init method and make sure that is being called before your method that adds KV's to the dictionary
I cannot reproduce this behavior. Here is my code:
ViewController.h:
ViewController.m:
No issues. If you are doing things this way, you cannot have problems. Here are the steps I would take to troubleshoot further:
您调用 tick: 的对象不再位于内存中并导致此崩溃。尝试看看为什么这个对象被释放。
The object on which you call tick: is not longer in memory and causes this crash. Try to see why this object is released.
看来这不是本地案例,所以你确定在上面综合了吗?并在标头中正确声明它?示例:
在标题中:
在类中:
It seems that this is not a local case, so did you make sure to synthesize it at the top? And declared it correctly in the header? Examples:
In header:
in class: