Reputation: 24481
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
}
Upvotes: 1
Views: 77
Reputation: 18253
The replies
property is never set - when you define setReplies:
, the @synthesize
directive does not create any setter method.
Upvotes: 0
Reputation: 32054
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];
}
}
Upvotes: 7
Reputation: 299265
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
.
Upvotes: 3