0

Well i am parsing data from json and I add them dynamically to an array. If i add 12 data of them, its ok. But when I am going to add an 13th I get this error(no matter what is the 13th):

EXC_BAD_ACCESS

Here is my code:

HomeView.h

#import <UIKit/UIKit.h>

@interface HomeView : UIViewController{  
    NSMutableData *responseData;
}

@property (nonatomic, strong) NSMutableData *responseData;

@end

and my implementation file:

@implementation HomeView
@synthesize responseData;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);

// convert to JSON
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
    NSString *parsed_track=[res objectForKey:@"data1"];
    [tracks_condition addObject:parsed_track];
    parsed_track=[res objectForKey:@"data2"];
    [tracks_condition addObject:parsed_track];
    parsed_track=[res objectForKey:@"data3"];
    [tracks_condition addObject:parsed_track];
    parsed_track=[res objectForKey:@"data4"];
    [tracks_condition addObject:parsed_track];
    parsed_track=[res objectForKey:@"data5"];
    [tracks_condition addObject:parsed_track];
    parsed_track=[res objectForKey:@"data6"];
    [tracks_condition addObject:parsed_track];

   parsed_track=[res objectForKey:@"data7"];
    [tracks_condition addObject:parsed_track];
    parsed_track=[res objectForKey:@"data8"];
    [tracks_condition addObject:parsed_track];
    parsed_track=[res objectForKey:@"data9"];
    [tracks_condition addObject:parsed_track];
    parsed_track=[res objectForKey:@"data10"];
    [tracks_condition addObject:parsed_track];
    parsed_track=[res objectForKey:@"data11"];
    [tracks_condition addObject:parsed_track];
    parsed_track=[res objectForKey:@"data12"];
    [tracks_condition addObject:parsed_track];

  // parsed_track=[res objectForKey:@"data13"]; //HERE COMES THE ERROR
   // [tracks_condition addObject:parsed_track];

}

I guess it is something with memory management but I cannot find out what.

1
  • That's not all of your code. You show your responseData property but don't use it, and the rest of it looks like a code chunk thrown in without context. Commented Aug 26, 2012 at 11:41

2 Answers 2

2

Are you sure there is an object for key @"data13" maybe this key does not exists and returns nil. in that case you need to check for nil and decide do you want to add it or not. If you want to add it just add [NSNull null] instead as you have to use an object that inherits NSObject.

Edit: please try to make it in a for loop too and construct the string with [NSString stringWithFormat.....]

Sign up to request clarification or add additional context in comments.

1 Comment

no, it exists. I have key until data 18 and everything I add more than 12 gets me that error.
1

EXC_BAD_ACCESS is because you are adding a nil object to the array, or the object has already been released by the time you add it to the array.

On another note, you can use a for loop for adding the objects to the array, rather than many lines of code:

for (NSUInteger idx = 1; idx <= [[res allKeys] count]; idx++) {

    if ([[[res allKeys] objectAtIndex:idx] rangeOfString:@"data"].location != NSNotFound)
        [tracks_condition addObject:[res objectForKey:[NSString stringWithFormat:@"data%d", idx]]];

}

or even better:

[self.questionInfo enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

        if ([key rangeOfString:@"data"].location != NSNotFound && obj != nil) {

            [tracks_condition addObject:obj];

        }

        else {

            if ([tracks_condition count] >= 18) {
                *stop = YES;

            }                   
        }

}];

1 Comment

Thanks for the code but adding in such way is not possible as I want only 18 datas out of 30 that are being fetched, and datas are not being stored in the same way my script prints them out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.