开发者

NSMutableArray removeObjectAtIndex: throws invalid argument exception

I'm writing an application to show some news from a portal. The news are fetched using a JSON file from the Internet and then stored into a NSMutableArray using the CoreData Model. Obviously a user can't delete the news from the JSON file on the Internet, but he can hide them locally. The problems come out 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];
}   

The line:

[dataSet removeObjectAtIndex:indexPath.row];

cause my apps to crash with the following error:

2010-07-12 19:08:16.021 ProvaVideo[284开发者_StackOverflow中文版:207] * -[_PFArray removeObjectAtIndex:]: unrecognized selector sent to instance 0x451c820 2010-07-12 19:08:16.022 ProvaVideo[284:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_PFArray removeObjectAtIndex:]: unrecognized selector sent to instance 0x451c820'

I'm trying to understand why it doesn't work but I can't. If I re-launch the app, the new is correctly logically cancelled. 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 net, I add them in the MutableArray:

[dataSet addObject:theNews];


Your NewsFetcher returns you an immutable array, not a mutable instance. Use the following instead for initialization:

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

An expression like A *a = (A*)b; only casts the pointer to a different type - it doesn't convert/change the actual type of the instance it points to.


Verify that dataSet is an NSMutableArray. The exception is getting thrown because it doesn't respond to removeObjectAtIndex.


jdot is correct dataSet must be NSMutableArray..

you must 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 you got from NewsFetcher and it won't crash on removing objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜