NSMutableArray removeObjectAtIndex: throws an invalid argument exception

I am writing an application to display news from the portal. The news is retrieved using the JSON file from the Internet, and then saved to NSMutableArray using the CoreData model. Obviously, the user cannot delete news from the JSON file on the Internet, but he can hide them locally. Problems arise here where I have the following code:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { if( !moc ){ moc = [[NewsFetcher sharedInstance] managedObjectContext]; } [[dataSet objectAtIndex:indexPath.row] setEliminata:[NSNumber numberWithBool:YES]]; NSError *error; if( ![moc save:&error] ){ NSLog( @"C'รจ stato un errore!" ); } [dataSet removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; } 

Line:

[DATASET removeObjectAtIndex: indexPath.row];

cause my applications to crash with the following error:

2010-07-12 19: 08: 16.021 ProvaVideo [284: 207] * - [_ PFArray removeObjectAtIndex:]: unrecognized selector sent to instance 0x451c820 2010-07-12 19: 08: 16.022 ProvaVideo [284: 207] * Termination of the application due to the uncaught exception "NSInvalidArgumentException", reason: '*** - [_PFArray removeObjectAtIndex:]: unrecognized selector sent to instance 0x451c820

I am trying to understand why this does not work, but I cannot. If I restart the application, the new one will correctly be logically canceled. Any suggestions ?? Thanks in advance.


Interface:

 @interface ListOfVideo : UITableViewController <NSFetchedResultsControllerDelegate> { NSMutableArray *dataSet; } @property (nonatomic, retain) NSMutableArray *dataSet; // In the .m file: @synthesize dataSet; 

Initialization in viewDidLoad :

 dataSet = (NSMutableArray *) [[NewsFetcher sharedInstance] fetchManagedObjectsForEntity:@"News" withPredicate:predicate withDescriptor:@"Titolo"]; [dataSet retain]; 

updateDatabase ... this is when checking for new news from the network, I add them to the MutableArray:

 [dataSet addObject:theNews]; 
+7
objective-c iphone
source share
3 answers

Your NewsFetcher returns you an immutable array, not a mutable instance. To initialize, use the following:

 NSArray *results = [[NewsFetcher sharedInstance] fetchManagedObjectsForEntity:@"News" withPredicate:predicate withDescriptor:@"Titolo"]; dataSet = [results mutableCopy]; 

In an expression like A *a = (A*)b; , only a pointer to another type is indicated - it does not convert / does not change the actual type of the instance to which it points.

+23
source share

Make sure the dataSet is an NSMutableArray. An exception is thrown because it does not respond to removeObjectAtIndex.

+5
source share

jdot the correct dataSet must be NSMutableArray ..

you have to do it this way.

  dataSet = [NSMutableArray arrayWithArray:[[NewsFetcher sharedInstance] fetchManagedObjectsForEntity:@"News" withPredicate:predicate withDescriptor:@"Titolo"]]; 

Now the dataSet is a mutable instance of the array that you received from NewsFetcher, and it will not crash when deleting objects.

+1
source share

All Articles