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

Change default collation/character set

mysql my.cnf

I know this has been answered before but the solutions I found does not apply to my system (I've tested them).

Running Remote Powershell Commandlet that wraps a process exits before completion

c# powershell powershell-v3.0 powershell-remoting visual-studio-test-runner

I have a custom PowerShell commandlet that I have created in C# that spins up some instances of vstest.console.exe and publishes the test results to a .trx file or to tfs.