Eric Martins
Eric Martins

Reputation: 460

EXC_BAD_ACCESS on NSMutableDictionary

I am beginning with iOS development, I have this code :

First of all I declare the listOfItems NSMutableArray:

@interface SAMasterViewController () {
    NSMutableArray *listOfItems;
}
@end

And now, here is the part the code that gives me an "EXC_BAD_ACCESS (code=1, address=0x5fc260000)" error. The error is given in the last line of the "individual_data" object.

listOfItems = [[NSMutableArray alloc] init];
for(NSDictionary *tweetDict in statuses) {
    NSString  *text          = [tweetDict objectForKey:@"text"];
    NSString  *screenName    = [[tweetDict objectForKey:@"user"] objectForKey:@"screen_name"];
    NSString  *img_url       = [[tweetDict objectForKey:@"user"] objectForKey:@"profile_image_url"];
    NSInteger unique_id      = [[tweetDict objectForKey:@"id"] intValue];
    NSInteger user_id        = [[[tweetDict objectForKey:@"user"] objectForKey:@"id"] intValue ];

    NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                            text, @"text_tweet",
                                            screenName,@"user_name",
                                            img_url, @"img_url",
                                            unique_id, @"unique_id",
                                            user_id, @"user_id", nil];
    [listOfItems addObject:individual_data];
}

Thanks in advance.

Upvotes: 1

Views: 2384

Answers (1)

Joe
Joe

Reputation: 57179

You can not put NSIntegers or any other non Objective-C class inside of an array or dictionary. You need to wrap them in an NSNumber.

NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                            text, @"text_tweet",
                                            screenName,@"user_name",
                                            img_url, @"img_url",
                                            [NSNumber numberWithInteger:unique_id], @"unique_id",
                                            [NSNumber numberWithInteger:user_id], @"user_id", nil];
//Or if you want to use literals
NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                            text, @"text_tweet",
                                            screenName,@"user_name",
                                            img_url, @"img_url",
                                            @(unique_id), @"unique_id",
                                            @(user_id), @"user_id", nil];

Upvotes: 5

Related Questions