2010-07-12 73 views
7

我正在編寫一個應用程序來顯示來自門戶的一些消息。該消息使用來自Internet的JSON文件獲取,然後使用CoreData模型存儲到NSMutableArray中。很明顯,用戶不能從互聯網上的JSON文件中刪除新聞,但他可以在本地隱藏它們。這些問題來到這裏,在這裏我有以下代碼:NSMutableArray removeObjectAtIndex:拋出無效參數異常

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

原因我的應用與以下錯誤崩潰:

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'

我試圖理解爲什麼它不起作用,但我不能。 如果我重新啓動應用程序,則新的程序在邏輯上被正確取消。 有什麼建議?提前致謝。


接口:

@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:

[dataSet addObject:theNews]; 
+1

你確定數據集是一個NSMutableArray嗎?如果它不響應removeObjectAtIndex:它可能只是一個NSArray。 – 2010-07-12 17:47:24

+1

如果它不響應該選擇器,它不是'NSMutableArray'。您可能無法正確創建它或錯誤地使用['copy'屬性](http://stackoverflow.com/questions/3220120/nsmutablearray-addobject-nsarrayi-addobject-unrecognized-selector-sent-to/3220137#3220137)。如果您懷疑嘗試使用'-isKindOfClass:'進行測試。 – 2010-07-12 17:57:52

+0

umble,它不是一個NSMutableArray,但是:@interface ListOfVideo:UITableViewController {NSMutableArray * dataSet; } @property(nonatomic,retain)NSMutableArray * dataSet; ... //在.m文件中@synthesize dataSet; – IssamTP 2010-07-12 19:44:25

回答

23

你的NewsFetcher返回你一個不可變的數組,而不是一個可變的實例。使用下面的,而不是進行初始化:

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

A *a = (A*)b;表達式只鑄指針到不同類型的 - 它不轉換/改變實際類型它指向的實例。

+0

LMAO它的工作原理。我是一個小白菜。 – IssamTP 2010-07-13 09:27:10

5

確認dataSet是一個NSMutableArray。拋出異常是因爲它不響應removeObjectAtIndex。

+0

嘟,,它不是NSMutableArray,但是: @interface ListOfVideo:UITableViewController { \t NSMutableArray * dataSet; } @property(nonatomic,retain)NSMutableArray * dataSet; ... //在.m文件中 @synthesize dataSet; – IssamTP 2010-07-12 19:42:33

+2

屬性爲可變數組不會使數組魔術變得可變。 – 2010-07-12 21:33:51

+0

@Joshua,雖然它不會「神奇地」使它變爲可變的,但它會阻止可變數組發生變異,因爲它沒有被正確定義。 – BlackHatSamurai 2013-05-18 22:51:29

1

jdot是正確的數據集必須是NSMutableArray的..

你都必須做這樣..

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

現在DataSet是從NewsFetcher得到了陣列的一個可變的實例,並贏得了」不會在刪除對象時崩潰。

+0

這解決了我的問題! :)謝謝@ pankaj :) – 2017-06-06 08:41:25

相關問題