开发者

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 UII开发者_StackOverflowmage'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...


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.


NSDictionary: objectForKey:

objectForKey: returns the value associated with key, or nil if no value is associated with key.

key :A string identifying the value. If nil, just return nil.

(1)you should know the key is not blank

(2)you should know the object is not blank and the objects are the same type ,because you shouldn't use different kinds of type in a NSDictionary.

e.g. {@"key1":NSString,@"key2":NSArray} //this is a demonstration of error

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜