Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble Adding Object To NSMutableArray in For Loop
    text
    copied!<p>I'm having some trouble adding an object to an NSMutableArray in a for loop. When I try it, I get an error:</p> <blockquote> <p><em>*</em> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' Blockquote</p> </blockquote> <p>I've looked at other questions very similar to this and they are solved because the array was immutable or they were reading off NSUserDefaults, however I am doing neither of those things.</p> <p>I download some JSON and parse it using NSJSONSelialization, and the idea is that I have an almost twitter-like stream of activity coming down from the top of the UITableView. Here's my code:</p> <pre><code>-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ if (isDownloadingFirstTime){ isDownloadingFirstTime = NO; dataArray = [Methods parseJSONDataWithData:data]; [mainTableView reloadData]; } else{ [refreshControl endRefreshing]; NSArray *tempArray = [Methods parseJSONDataWithData:data]; for (int num = 0; num &lt; tempArray.count; num++){ [dataArray addObject:[tempArray objectAtIndex:num]]; NSArray *indexPathArray = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; [mainTableView beginUpdates]; [mainTableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; [mainTableView endUpdates]; } } data = [NSMutableData data]; } </code></pre> <p>I get the exception on the first line of the for loop, when I try to addObject:</p> <p>I in no way am using NSUserDefaults in this class and I'm 100% sure that dataArray is an NSMutableArray.</p> <p>Any help or advice is appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload