[英]Unsolved MATTER with data core in iOS iPhone App
我已盡力解決此問題,但始終出現以下錯誤:
-[__ NSCFConstantString ling]:無法識別的選擇器已發送到實例0x12f80b0
我想做的是使用AlertView中的文本向核心數據和表視圖中添加行,因此啟動AlertView並用戶輸入新語言的名稱,然后將保存AlertView中的文本用戶單擊保存時將其添加到核心數據並添加到表視圖中。
在表格視圖中,這是相關的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Languages *languagesDict = (Languages *)[languagesArray objectAtIndex:indexPath.row];
cell.textLabel.text = [languagesDict ling];
return cell;
}
在alertview中,這是單擊“保存”按鈕時的代碼:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
NSString *tempText = [alertView textFieldAtIndex:0].text;
if(!languagesArray)
{
languagesArray = [[NSMutableArray alloc]init];
}
[languagesArray insertObject:tempText atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
Languages *languagesDict = [NSEntityDescription insertNewObjectForEntityForName:@"Languages" inManagedObjectContext:_managedObjectContext];
[languagesDict setLing:tempText];
NSError *error = nil;
if (![_managedObjectContext save:&error])
{
}
}
}
有人可以告訴我我在做什么錯嗎?
您正在將NSString
對象插入您的languagesArray
。
當您嘗試將對象提取回去時,在以下行中:
Languages *languagesDict = (Languages *)[languagesArray objectAtIndex:indexPath.row];
您正在將這些NSString
對象(出於某種原因)強制轉換為Languages
對象。 然后嘗試對已獲取的對象調用ling
方法。
但是ling
方法在NSString
中不存在,因此這就是您遇到運行時崩潰和錯誤消息的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.