antonio'Smith
antonio'Smith

Reputation: 33

Creating objects in a loop, but ivar / instance variable not keeping state

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"];
}

Upvotes: 3

Views: 218

Answers (1)

Carl Veazey
Carl Veazey

Reputation: 18363

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".

Upvotes: 4

Related Questions