简体   繁体   English

iOS iPhone App中具有数据核心的未解决的MATTER

[英]Unsolved MATTER with data core in iOS iPhone App

I tried my best to solve this problem but I keep getting the following error: 我已尽力解决此问题,但始终出现以下错误:

-[__NSCFConstantString ling]: unrecognized selector sent to instance 0x12f80b0 -[__ NSCFConstantString ling]:无法识别的选择器已发送到实例0x12f80b0

what I am trying to do is to add line to core data and to table view with a text from the alertview, so a fire up an alertview and the user put the name of a new language, then the text in the alertview will be saved to core data and added to table view when the user click save. 我想做的是使用AlertView中的文本向核心数据和表视图中添加行,因此启动AlertView并用户输入新语言的名称,然后将保存AlertView中的文本用户单击保存时将其添加到核心数据并添加到表视图中。

In the table view, this is the relevant code: 在表格视图中,这是相关的代码:

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

And in the alertview this is the code when "save" button is clicked: 在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])
        {
        }
    }
  }

Can someone tell me what is the wrong thing I am doing?? 有人可以告诉我我在做什么错吗?

You're inserting NSString objects into your languagesArray . 您正在将NSString对象插入您的languagesArray

When you try and extract the objects back out, in the line: 当您尝试将对象提取回去时,在以下行中:

Languages *languagesDict = (Languages *)[languagesArray objectAtIndex:indexPath.row]; 

You're casting those NSString objects (for some reason) to be Languages objects. 您正在将这些NSString对象(出于某种原因)强制转换为Languages对象。 Then you try to call the ling method on the object you've fetched. 然后尝试对已获取的对象调用ling方法。

But the ling method doesn't exist in NSString , so that's how you're getting your run-time crash and your error message. 但是ling方法在NSString中不存在,因此这就是您遇到运行时崩溃和错误消息的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM