NSMutableArray removeObjectAtIndex:抛出无效的参数异常 [英] NSMutableArray removeObjectAtIndex: throws invalid argument exception

查看:333
本文介绍了NSMutableArray removeObjectAtIndex:抛出无效的参数异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序来显示门户网站的一些新闻。使用来自Internet的JSON文件获取新闻,然后使用CoreData模型将其存储到NSMutableArray中。显然,用户无法从Internet上的JSON文件中删除新闻,但他可以在本地隐藏它们。问题出现在这里,我有以下代码:

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];
}   

行:

[dataSet
removeObjectAtIndex:indexPath.row];

[dataSet removeObjectAtIndex:indexPath.row];

导致我的应用崩溃出现以下错误:

cause my apps to crash with the following error:


2010-07-12 19:08:16.021
ProvaVideo [284:207] * - [_ PFArray
removeObjectAtIndex:]:无法识别的
选择器发送到实例0x451c820
2010-07-12 19:08:16.022
ProvaVideo [284:207] *
由于未捕获的异常终止
app
'NSInvalidArgumentException',原因:
'*** - [_ PFArray removeObjectAtIndex:]:
无法识别的选择器发送到实例
0x451c820 '

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] * 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 ListOfVideo : UITableViewController <NSFetchedResultsControllerDelegate> { 
    NSMutableArray *dataSet;
} 
@property (nonatomic, retain) NSMutableArray *dataSet;

// In the .m file:
@synthesize dataSet;

中的初始化viewDidLoad

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

updateDatabase ...这是在检查时对于来自网络的新消息,我将它们添加到MutableArray中:

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

[dataSet addObject:theNews];


推荐答案

您的 NewsFetcher 返回一个不可变数组,而不是一个可变实例。使用以下代码进行初始化:

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];

这样的表达式A * a =(A *)b; 仅将指针强制转换为其他类型 - 它不会转换/更改它指向的实例的实际类型。

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.

这篇关于NSMutableArray removeObjectAtIndex:抛出无效的参数异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆