我正在使用ARC并以iOS 5为目标。我已经在我的一个视图控制器头中声明了一个NSMutableArray ivar,在viewDidLoad中初始化它,并且我试图在我的一些视图控制器的方法中操作它。然而,它表现得非常奇怪。当我像“addObject:”一样向它发送消息,或者尝试将其设置为等于在方法的本地范围内创建的NSMutableArray时,它将消息发送到我声明的其他ivars。
例如,我一直遇到的错误之一是 - [PullToRefreshView count]:发送到实例的无法识别的选择器。 PullToRefreshView是我的另一个ivars,但在我的代码中,我显然是将计数消息发送到我的NSMutableArray ivar。当试图将对象添加到ivar NSMutableArray时,它的计数(在调试器中)保持为0.我一直在搜索我的代码以寻找我可能忽略的东西,但它似乎只是某种ARC内存管理侥幸。有任何想法吗?这是我的代码的相关区域:
MyViewController.h
@interface MyViewController : UIViewController
{
PullToRefreshView *pull;
NSMutableArray *commentsSharesMerged;
}
@property (nonatomic, retain) PullToRefreshView *pull;
@property (nonatomic, retain) NSMutableArray *commentsSharesMerged;
- (void)refreshComments;
- (void)refreshShares;
@end
MyViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
commentsSharesMerged = [[NSMutableArray alloc] init];
pull = [[PullToRefreshView alloc] initWithScrollView:(UIScrollView *)self.suggestionTable];
[pull setDelegate:self];
[self.suggestionTable addSubview:pull];
}
- (void)refreshComments
{
NSURL *url = [NSURL URLWithString:@"http://example.com/comments.json"];
__unsafe_unretained __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
NSString *responseString = [request responseString]; // Use when fetching text data
NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
id jsonObject = [[CJSONDeserializer deserializer] deserialize:responseData error:nil];
NSLog(@"Connection JSON: %@", jsonObject);
NSMutableArray *commentsArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [jsonObject count]; i++)
{
NSDictionary *currentCommentData = [jsonObject objectAtIndex:i];
Comment *comment = [[Comment alloc] init];
comment.identifier = [[currentCommentData objectForKey:@"id"] intValue];
comment.firstName = [currentCommentData objectForKey:@"first_name"];
comment.lastName = [currentCommentData objectForKey:@"last_name"];
comment.commentText = [currentCommentData objectForKey:@"comment"];
[commentsArray addObject:comment];
}
self.commentsSharesMerged = [commentsArray mutableCopy];
// I've also tried to add the object using something like this
//for (Comment *comment in commentsArray)
// [commentsSharesMerged addObject:comment];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"%@", error);
}];
[request startAsynchronous];
}
答案 0 :(得分:0)
我找到了解决问题的方法。它可以归因于LLDB调试中的某种内存错误。我将我的计划转移到了GDB,并修复了我的ivar分配问题。