Search code examples
ioscrashsigabrt

objectForKey crash? iOS


I am trying to use the following code to load a UIButton and UITextField with information based upon a UISegmentedControl's current segment that is clicked but it is causing a SIGABRT crash. Here is the code:

- (void)updateInfo {

NSLog(@"count1:%d", [self.accounts count]);

[self saveInfo];

NSLog(@"count2:%d", [self.accounts count]);

NSDictionary *dict = [self.accounts objectAtIndex:0];
NSData *imageData = [dict objectForKey:@"ProfileImage"];
UIImage *imageProfile = [UIImage imageWithData:imageData];
[image1 setImage:imageProfile];

NSDictionary *dict2 = [self.accounts objectAtIndex:1];
NSData *imageData2 = [dict2 objectForKey:@"ProfileImage"];
UIImage *imageProfile2 = [UIImage imageWithData:imageData2];
[image2 setImage:imageProfile2];

if ([self.accounts objectAtIndex:accountSC.selectedSegmentIndex] != nil) {
    NSDictionary *dict = [self.accounts objectAtIndex:accountSC.selectedSegmentIndex];
    //NSString *name = [dict objectForKey:@"Name"];
    NSString *name = [accountSC titleForSegmentAtIndex:accountSC.selectedSegmentIndex];
    [Name setText:name];
    NSData *imageData = [dict objectForKey:@"ProfileImage"];
    UIImage *imageProfile = [UIImage imageWithData:imageData];
    [pictureButton setImage:imageProfile forState:UIControlStateNormal];
}
else {
    Name.text = nil;
    [pictureButton setImage:nil forState:UIControlStateNormal];
}

}

The first & second big block of code is temporary because I wanted to see the UIImage's based upon different objectAtIndex numbers.

Here is the console crash log:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x2cc08'

Any reason why this can be happening? Do I need to post any other code I am using?

I really need help on this, I have been pulling my hair out!!!

Edit1: I am using this code, also you were talking about isKindOfClass right? Anyway this is the code:

NSDictionary *dict = [self.accounts objectAtIndex:1];
if ([dict isKindOfClass:[NSDictionary class]]) {
    NSLog(@"YES");
}
else {
    NSLog(@"NO");
}

Im testing now...


Solution

  • You're sending objectForKey: to an NSString object, specifically one that you've typed directly into your code somewhere in @"..." form rather than one you've created programmatically (or had created programmatically for you). Someone is putting something other than a dictionary into self.accounts.