简体   繁体   中英

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

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.

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:

  - (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 .

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. Then you try to call the ling method on the object you've fetched.

But the ling method doesn't exist in NSString , so that's how you're getting your run-time crash and your error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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