Weird Memory Management Occurrence iOS

For some reason in my below code, the replies array is NSLogging the correct description, but the comment.replies array is NSLogging null.

I immediately presumed that this was due to a memory management issue within my code, but I don't believe that that is true.

Please can you tell me why this is occurring?

- (TBComment *) dictionaryToComment:(NSDictionary *)dict {
    TBComment *comment = [[TBComment alloc] init];
    [comment setBody:[dict objectForKey:@"body"]];
    [comment setCommentID:[dict objectForKey:@"id"]];
    [comment setCreated_at:[dict objectForKey:@"created_at"]];
    [comment setUpdated_at:[dict objectForKey:@"updated_at"]];
    [comment setUser:[self dictionaryToUser:[dict objectForKey:@"user"]]];
    NSMutableArray *replies = nil;
    if ([[dict allKeys] containsObject:@"replies"]) {
        replies = [[NSMutableArray alloc] init];
        for (NSDictionary *reply in [dict objectForKey:@"replies"]) {
            NSLog(@"in");
            [replies addObject:[self dictionaryToComment:reply]];
        }
    }
    if (replies != nil) {
        [comment setReplies:replies];
        NSLog(@"COMMENT REPLIES = %@", comment.replies);
        NSLog(@"REPLIES = %@", replies);
        [replies release];
    }
    return [comment autorelease];
}

Console ->

2011-11-30 21:25:14.980 Timbrr[2379:f803] in
2011-11-30 21:25:14.980 Timbrr[2379:f803] COMMENT REPLIES = (null)
2011-11-30 21:25:14.980 Timbrr[2379:f803] REPLIES = (
    "<TBComment: 0x68dbeb0>"
)


- (void) setReplies:(NSArray *)_replies {
    hasReplies = (_replies == nil ? NO : ([_replies count] == 0 ? NO : YES));
    //replies is synthesised
}

Answers


I would suspect that either comment is nil (though this would require explicit nil-returning code in TBComment, which is possible, but unusual), or that -replies or -setReplies: are incorrectly implemented. Do you have custom implementations for those?

Your implementation of setReplies: never sets _replies.


After seeing your implementation of setReplies:, I don't think you quite understand how @synthesize works.

@synthesize replies; will generate a getter and a setter for this instance variable. BUT since you're overriding it (and improperly) the synthesized setter is being tossed aside. (In fact, no setter is being created for you at all, since you wrote one yourself.)

The root issue is that in your implementation of setReplies:, you're not actually assigning the value of your replies instance variable to the parameter of the setter.

What I think you want is:

- (void) setReplies:(NSArray *)_replies {
    hasReplies = (_replies == nil ? NO : ([_replies count] == 0 ? NO : YES));
    // How is your ivar defined in the header file? As _replies, or replies?
    if (replies != _replies) {
        [replies release];
        replies = [_replies retain];
    }
}

Need Your Help

Emacs ESS indentation and auto-completion

r emacs ess

There are a number of useful variables to control TAB key indentation and completion in Emacs (R) code chunks, when using ESS mode.

How to use ng-repeat $index to find element in array

javascript arrays angularjs angularjs-ng-repeat

In my $scope, I have a property that's an array of cars appropriately name $scope.cars. User has ability to delete a car. I pass the $index (created by ng-repeat) of the car as a parameter to delete

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.