setObject:ForKey: crash?



我在控制台遇到这个崩溃:

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"];

这里有什么不对劲吗?有什么问题吗?

Edit1:当我这样做来调用一个方法时,它会崩溃:代码:

if(actionSheet.tag == 1) {
            [self showAchievements];
        }

这是showachievement方法:代码:

- (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中连接所有东西吗?我确信它在那个方法中崩溃了,我不知道为什么。

有人有什么想法吗?

编辑2:我在这里得到它,在我的控制台上,就在它崩溃之前,它有一行说错过的方法。所以我搜索这个NSLog在哪里它在GameCenterManager中。我想这是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");
    }
}

那么从这段代码中你能看出什么是错的吗?

谢谢!

我猜答案就在你发布的代码中:

reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil key'

不能在字典中插入nil键。你是在做像[myDictionary setObject:foo forKey:bar]这样的东西吗,其中'bar'是nil?

顺便说一下,你也不能在字典中插入nil值,但这似乎不是错误原因所指示的。可以调用[myDictionary setValue:nil forKey:bar](只要bar不是nil)—这将从字典中删除键。如果您想要指示一个"nil"值,您可以将NSNull对象插入到字典中。但是键——键必须总是非nil

从日志看来,键profileImage等于nil,这意味着在你的NSDictionary中没有该键的数据。

最新更新