I'm using ARC and targeting iOS 5. I've declared an NSMutableArray ivar in one of my view controller's header, initialized it in viewDidLoad, and I'm attempting to manipulate it in some of my view controller's methods. However, it is behaving very weird. As I send messages to it like "addObject:", or try to set it to equal to an NSMutableArray, created in the local scope of a method, it is sending the message to other ivars I've declared.
For example, one of the errors I keep getting is -[PullToRefreshView count]: unrecognized selector sent to instance. PullToRefreshView is another one of my ivars, but in my code, I am clearly sending the count message to my NSMutableArray ivar. When trying to add an object to the ivar NSMutableArray it's count (in debugger) stays at 0. I keep searching my code for something I may have overlooked, but it just seems like some kind of ARC memory management fluke. Any ideas? Here's the relevant areas of my code:
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];
}