3

I have a method that creates a dictionary from NSJSONSerialization class. I then enumerate the json, and create objects to store state for each instance.

- (void)fetchedData:(NSData *)responseData {

    NSError* error;
    NSDictionary *json = [NSJSONSerialization
                          JSONObjectWithData:responseData
                          options:kNilOptions
                          error:&error];

    NSArray *moviesJson = [json objectForKey:@"movies"];

    for(NSDictionary *element in moviesJson)
    {
         RABMovie *myMovie = [[RABMovie alloc] initWithJson:element];
       // RABMovie *myMovie = [RABMovie new];
       // myMovie.json = element;

        [_myMovieNames addObject: myMovie];
        myMovie = nil;

    }

    [self.movieTableView reloadData]; 
}

Problem: I want to create my object by passing in element in the allocator, however when I do this, my UITTableView rows all contain the same movie data. It is always the last item, leading me to believe I am working with only one memory address, and the last update affects the whole collection.

If I uncomment the code to use the json as a property instead of a alloc param, I no longer have the issue and all works well. I've also tried creating a completely new NSDictionary via a deep copy of element to no avail.

Does someone know why this is happening? BTW, I am using ARC. -Thanks for the time.

Edit: Added more code. I've included a property movieName to illustrate how I use the ivar _json.

@implementation RABMovie

NSDictionary *_json;
- (id) initWithJson: (NSDictionary*) jsonAsDictionary
{
    if (self = [super init])
    {

        _json =  jsonAsDictionary;
    }

    return self;
}

- (NSString*) movieName
{
    return [_json objectForKey:@"title"];
}
2
  • 4
    Can you post the code for the RABMovie initializer (initWithJson) ? Commented Sep 23, 2012 at 15:26
  • Yes, you should post more code, there's nothing wrong in the code you posted -- it should work (assuming that moviesJson contains what you think it does. You should log element inside your for-in loop to make sure it does). Commented Sep 23, 2012 at 16:05

1 Answer 1

4

I think you meant to declare _json as an instance variable. Instead it's a globally visible (at least within that class) variable - not 100% sure on the scoping rules, but regardless, it's not an instance variable - it's a single variable shared by all instances! Try this instead:

@implementation RABMovie {
    NSDictionary *_json;
}

/* ...rest of class */

@end

Putting it inside the curly braces after the @implementation directive makes it an instance variable. Hope this helps!

EDIT: Do you have a property called json on RABMovie already? Then you can skip the instance declaration altogether and the compiler will generate the an instance variable for you. That's probably happening already actually, which is why it works when you go through the property - it's accessing the ivar rather than the "global".

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

1 Comment

Darn, I meant to have _json as ivar, but forgot the curly braces. That solved the problem. I understand now, of course if it had global scope it will retain only the last value that was set on it. Hence, why my table view rows had all the same data. Thanks!

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.