max*_*ax_ 1 memory-management objective-c ios
由于某些原因,在我的下面的代码中,replies数组是NSLogging正确的描述,但comment.replies数组是NSLoggingnull.
我立即假设这是由于我的代码中的内存管理问题,但我不相信这是真的.
请你能告诉我为什么会这样吗?
- (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];
}
Run Code Online (Sandbox Code Playgroud)
控制台 - >
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
}
Run Code Online (Sandbox Code Playgroud)
看到你的实施后setReplies:,我不认为你很明白它是如何@synthesize工作的.
@synthesize replies;将为此实例变量生成一个getter和一个setter.但是因为你正在覆盖它(并且不正确),所以合成的setter被抛到了一边.(事实上,根本没有为你创建setter,因为你自己写了一个.)
根本问题是,在实现中setReplies:,实际上并没有将replies实例变量的值赋给setter的参数.
我认为你想要的是:
- (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];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |