setObject:ForKey: 崩溃?
我在控制台中遇到此崩溃:
2011-08-27 23:26:45.041 App[1672:3117] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil key'
*** First throw call stack:
(0x3231c8c3 0x362ee1e5 0x3231c7bd 0x3231c7df 0x32289679 0x3052e865 0x3220fd55 0x3221b7a3 0x3345e4b7 0x3345e38c)
terminate called throwing an exception[Switching to process 10243 thread 0x2803]
[Switching to process 10243 thread 0x2803]
我认为在崩溃时与此崩溃相关的唯一会触发的行是此行:
UIImage *photo = [self.selectedDictionary objectForKey:@"ProfileImage"];
这里看起来有什么问题吗?可能是什么问题?
编辑1: 当我这样做来调用方法时,它崩溃了: 代码:
if(actionSheet.tag == 1) {
[self showAchievements];
}
这是showAchievements方法: 代码:
- (void)showAchievements {
GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init];
if (achievements != nil)
{
achievements.achievementDelegate = self;
[self presentModalViewController:achievements animated:YES];
}
}
- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
[self dismissModalViewControllerAnimated:YES];
[viewController release];
}
这次崩溃可能是因为我没有正确连接 iTC 中的所有内容吗?我确信它会在该方法中崩溃,但我不知道为什么。
有人有什么想法吗?
Edit2:我正在认真对待它,在我的控制台中,就在它崩溃之前,它有一行说“错过了方法”。所以我搜索这个 NSLog 在哪里,它在 GameCenterManager.m 中,我认为这是 Game Center 必需的文件。方法如下:
- (void) callDelegate: (SEL) selector withArg: (id) arg error: (NSError*) err
{
assert([NSThread isMainThread]);
if([delegate respondsToSelector: selector])
{
if(arg != NULL)
{
[delegate performSelector: selector withObject: arg withObject: err];
}
else
{
[delegate performSelector: selector withObject: err];
}
}
else
{
NSLog(@"Missed Method");
}
}
那么从该代码中您看出什么问题了吗?
谢谢!
I am getting this crash in the console:
2011-08-27 23:26:45.041 App[1672:3117] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil key'
*** First throw call stack:
(0x3231c8c3 0x362ee1e5 0x3231c7bd 0x3231c7df 0x32289679 0x3052e865 0x3220fd55 0x3221b7a3 0x3345e4b7 0x3345e38c)
terminate called throwing an exception[Switching to process 10243 thread 0x2803]
[Switching to process 10243 thread 0x2803]
The only line that I think would fire at the time it crashes that relates to this crash is this line:
UIImage *photo = [self.selectedDictionary objectForKey:@"ProfileImage"];
Does anything look wrong here? What could be the problem?
Edit1:
It is crashing when I do this to call a method:
Code:
if(actionSheet.tag == 1) {
[self showAchievements];
}
This is the showAchievements method:
Code:
- (void)showAchievements {
GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init];
if (achievements != nil)
{
achievements.achievementDelegate = self;
[self presentModalViewController:achievements animated:YES];
}
}
- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
[self dismissModalViewControllerAnimated:YES];
[viewController release];
}
Can this crash be because I have not properly hooked everything up in iTC? I am definitely sure it crashes in that method and I do not know why.
Does anyone have any ideas?
Edit2: I am getting down to it here, in my console, right before it crashes it has a line saying Missed Method. So I search for where this NSLog is and it is in GameCenterManager.m, a file necessary for Game Center I think. Here is the method:
- (void) callDelegate: (SEL) selector withArg: (id) arg error: (NSError*) err
{
assert([NSThread isMainThread]);
if([delegate respondsToSelector: selector])
{
if(arg != NULL)
{
[delegate performSelector: selector withObject: arg withObject: err];
}
else
{
[delegate performSelector: selector withObject: err];
}
}
else
{
NSLog(@"Missed Method");
}
}
So from that code do you see whats wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜答案就在您发布的代码中:
您不能将 nil 键插入字典中。你是否在做类似 [myDictionary setObject:foo forKey:bar] 的事情,其中“bar”为零?
顺便说一句,您也不能将 nil 值插入字典中,但这似乎并不是错误原因所指示的。您可以调用
[myDictionary setValue:nil forKey:bar]
(只要bar不为零) - 这会从字典中删除键。如果您想指示“nil”值,您可以将NSNull
对象插入字典中。但是键——键必须始终非零。I'd guess the answer is right there in the code you posted:
You can't insert a nil key into a dictionary. Are you doing something like [myDictionary setObject:foo forKey:bar] where 'bar' is nil?
Incidentally, you also can't insert nil values into a dictionary, but that doesn't appear to be what the error reason indicates. You can call
[myDictionary setValue:nil forKey:bar]
(as long as bar isn't nil) - this removes the key from the dictionary. And if you want to indicate a "nil" value, you can insert theNSNull
object into a dictionary. But keys -- keys must always be non-nil.从日志来看,键
profileImage
等于nil
这意味着您的 NSDictionary 中没有该键的数据。From the log it seems like the key
profileImage
equalsnil
which means that there is no data for that key in your NSDictionary.