获取数据时核心数据SIGABRT?

时间:2014-09-11 23:46:18

标签: ios objective-c core-data sigabrt

在我的应用程序中,我有一个功能,允许用户创建他们自己的过敏成分表视图。这是我创建填充它的数组的方法以及填充它的方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    badIngredientsArray = [[NSMutableArray alloc] init];
    rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Add"
                                               style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.rightBarButtonItem = rightButton;

    rightButton.target = self;
    rightButton.action = @selector(addRow);

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSManagedObjectContext *context = [self managedObjectContext];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"AllergicIngredient" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    NSError *error;

    NSArray *ingredientsArray = [context executeFetchRequest:fetchRequest error:&error];
    badIngredientsArray = [NSMutableArray arrayWithArray:ingredientsArray];
}

-(void)addRow
{
    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Add a Bad Ingredient" message:@"Type the name of the ingredient" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil];
    myAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    [myAlertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSManagedObjectContext *context = [self managedObjectContext];
    AllergicIngredient *allergic = [NSEntityDescription insertNewObjectForEntityForName:@"AllergicIngredient" inManagedObjectContext:context];

    NSString *enteredString = [[alertView textFieldAtIndex:0] text];

    [allergic setValue:enteredString forKey:@"name"];

    NSError *error;

    if (![context save:&error])
    {
        NSLog(@"Couldnt find the save %@", error.localizedDescription);
    }    
    else
    {
        NSLog(@"It saved properly");
    }

    [badIngredientsArray addObject:enteredString];
    [self.tableView reloadData];
}

好像当我删除以下代码时:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"AllergicIngredient" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSError *error;

NSArray *ingredientsArray = [context executeFetchRequest:fetchRequest error:&error];
badIngredientsArray = [NSMutableArray arrayWithArray:ingredientsArray];

功能正常,但它失去了核心数据方面,如果您删除应用程序并重新启动它,表视图为空。所以我似乎无法解决上述代码的错误。对于想知道SIGABRT错误发生在哪里的所有人来说,就在这一行:

cell.textLabel.text = [badIngredientsArray objectAtIndex:indexPath.row];

更新:

我的表格查看方法:

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

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


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

   if (cell == nil)
 {
   cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
 }

cell.textLabel.text = [badIngredientsArray objectAtIndex:indexPath.row];

return cell;
}

1 个答案:

答案 0 :(得分:1)

[badIngredientsArray objectAtIndex:indexPath.row]不会返回NSString,而是返回NSManagedObject

tableView:cellForRowAtIndexPath:中,尝试以下代码以获取name属性:

AllergicIngredient *object = (AllergicIngredient *)[badIngredientsArray objectAtIndex:indexPath.row];
cell.textLabel.text = object.name;
//or
//NSManagedObject *object = (NSManagedObject *)[badIngredientsArray objectAtIndex:indexPath.row];
//cell.textLabel.text = [object objectForKey:@"name"];