setObject:ForKey:crash? [英] setObject:ForKey: crash?
问题描述
我在控制台中收到此崩溃:
2011-08-27 23:26:45.041应用[1672: 3117] ***由于未捕获异常而终止应用程序'NSInvalidArgumentException',reason:' - [__ NSCFDictionary setObject:forKey:]:尝试插入nil键'
***第一次调用堆栈:
终止调用抛出异常[切换到进程10243线程0x2803]
[切换到进程10243线程0x2803]
我认为在崩溃时,与这个崩溃有关的唯一一行是这行:
UIImage * photo = [self.selectedDictionary objectForKey:@ProfileImage];
这里有什么问题吗?可能是什么问题?
Edit1:
当我这样做来调用一个方法时崩溃:
代码:
if(actionSheet.tag == 1){
[self showAchievements];
}
这是showAchievements方法:
代码:
- (void)showAchievements {
GKAchievementViewController * achievement = [[GKAchievementViewController alloc] init];
if(achievements!= nil)
{
achievement.achievementDelegate = self;
[self presentModalViewController:achievements animated:YES];
}
}
- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
[self dismissModalViewControllerAnimated:YES];
[viewController release];
}
这次崩溃可能是因为我没有在iTC中正确挂钩
有没有任何想法?
Edit2:我在这里,在我的控制台,就在它崩溃之前,它有一行缺少方法。所以我搜索这个NSLog的位置,它在GameCenterManager.m,一个游戏中心我认为必要的文件。这里是方法:
- (void)callDelegate:(SEL)selector withArg:(id)arg error:(NSError * )err
{
assert([NSThread isMainThread]);
if([delegate respondingToSelector:selector])
{
if(arg!= NULL)
{
[delegate performSelector:selector withObject:arg withObject:err] ;
}
else
{
[delegate performSelector:selector withObject:err];
}
}
else
{
NSLog(@Missed Method);
}
}
那么从代码中你会看到什么错误? / p>
谢谢!
在您发布的代码中:
原因:' - [__ NSCFDictionary setObject:forKey:]:尝试插入nil键'
不能将nil键插入字典。你在做什么像[myDictionary setObject:foo forKey:bar]其中'bar'是nil?
顺便说一下,你也不能将nil值插入字典,但是这似乎不是错误原因所指示的。您可以呼叫 [myDictionary setValue:nil forKey:bar]
(只要bar不为nil) 。如果你想指明一个nil值,你可以将 NSNull
对象插入字典。但是键 - 键必须始终为非零。
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!
I'd guess the answer is right there in the code you posted:
reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil key'
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 the NSNull
object into a dictionary. But keys -- keys must always be non-nil.
这篇关于setObject:ForKey:crash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!