setObject:ForKey: crash?

佐手、 提交于 2019-12-13 02:00:27

问题


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!


回答1:


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.




回答2:


From the log it seems like the key profileImage equals nil which means that there is no data for that key in your NSDictionary.



来源:https://stackoverflow.com/questions/7218927/setobjectforkey-crash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!