2017-02-03 37 views
0

我試圖從核心數據中提取結果以在視圖加載時顯示在表視圖中。請求確實獲取結果,但在視圖加載後立即崩潰。獲取結果崩潰(使用persistentContainer) - 核心數據目標C

與理由:「 - [__ NSArrayI isEqualToString:]:發送到實例

自從引入永久集裝箱無法識別的選擇,我找不到任何有關如何使用Objective C.

使用它的任何引用

我有一個簡單的核心數據模型, 實體 - '項目' 與屬性 - '姓名'

// // ViewController.m

@interface ViewController() 
{ 
NSMutableArray *listArray; 
AppDelegate *delegate; 
NSManagedObjectContext *context; 
NSMutableArray *resultListArray; 
} 
@end 

- (void)viewDidLoad { 
[super viewDidLoad]; 
listArray = [[NSMutableArray alloc]init]; 
resultListArray = [[NSMutableArray alloc]init]; 
[self fetchItems]; 
} 

的TableView數據源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return [listArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

if (resultListArray) { 
    cell.textLabel.text = [resultListArray objectAtIndex:indexPath.row]; 
} 

cell.textLabel.text = [listArray objectAtIndex:indexPath.row]; 
return cell; 
} 

獲得託管上下文

- (NSManagedObjectContext *)managedObjectContext { 

delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate]; 
context = [[delegate persistentContainer]viewContext]; 

NSLog(@"ManagedContext Created Successfully"); 

return context; 
} 

保存到核心數據

- (void) saveItemMethod:(NSString*)name { 

context = [self managedObjectContext]; 

NSManagedObject *task = [[Item alloc]initWithContext:context]; 

[task setValue:name forKey:@"name"]; 

NSString *itemString = [task valueForKey:@"name"]; 

[listArray addObject:itemString]; 

[delegate saveContext]; 

NSLog(@"Save successful"); 
NSLog(@"%@", listArray); 
} 

抓取結果

- (void) fetchItems { 

context = [self managedObjectContext]; 

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Item"]; 
// request.resultType = NSDictionaryResultType; 

NSError *error = nil; 
NSManagedObject *result = (NSManagedObject*)[context executeFetchRequest:request error:&error]; 

NSString *resultString = [result valueForKey:@"name"]; 

[resultListArray addObject:resultString]; 

NSLog(@"Fetch successful"); 
NSLog(@"%@", resultListArray); 

[self.tableView reloadData]; 
} 
+0

如果您想了解NSPersistentContainer,請觀看本講座:https://vimeo.com/89370886。本次講座來自NSPersistentContainer之前,但仍然非常相關。基本上NSPersistentContainer設置了演講推薦的核心數據棧。 –

+0

您需要在調試器中運行您的應用程序並打開異常斷點。然後你就可以看到你的應用崩潰的確切位置。一旦我們看到應用程序崩潰的代碼,我們可以調試它出錯的原因。 –

+0

@DaveWeston謝謝你戴夫。我無法在表格視圖中顯示數據。現在解決了。 – Rajxelton

回答

1

錯誤消息表明isEqualToString:方法在NSArray對象上調用 - 顯然不起作用(isEqualToString:NSString方法)。

因此,您的代碼將數組視爲字符串。該問題的根源在於這裏,在fetchItems代碼:

NSManagedObject *result = (NSManagedObject*)[context executeFetchRequest:request error:&error]; 
NSString *resultString = [result valueForKey:@"name"]; 
[resultListArray addObject:resultString]; 

第一行是錯誤的:executeFetchRequest返回NSManagedObjects的陣列(即使在陣列中只有一個對象)。因此,您可以使用:

NSArray *result = [context executeFetchRequest:request error:&error]; 
self.resultListArray = [[result valueForKey:@"name"] mutableCopy]; 
+0

感謝您的回覆。我早些時候再次嘗試過這一點。仍然與原因崩潰:'*** - [__ NSArrayM objectAtIndex:]:索引0超出空數組的界限',顯示數組爲空。 但在fetchItems方法中,logging resultListArray在輸出控制檯中給出了完整的數組項目。 – Rajxelton

+0

我繼續並在numberOfRows方法內將if(resultListArray)替換爲if([resultListArray count]!= 0)。應用程序沒有運行,但沒有在表格視圖中顯示任何列表 – Rajxelton

+0

@Rajxelton我不確定爲什麼你有兩個數組,listArray和resultListArray,但我懷疑突出的問題是你在某個地方使用了錯誤的數組。 – pbasdf