将UILabel结果保存到CoreData中

时间:2012-01-17 00:31:09

标签: ios core-data save

我在ViewController上有一个选择器,结果打印在标签上。我在将这些结果保存到CoreData时遇到问题,收到错误代码

"[<__NSCFConstantString 0xf488> valueForUndefinedKey:]: this class is not key value coding-compliant for the key attribute.'.

以下是打印出标签的代码:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    mlabel.text= [arrayNo objectAtIndex:row];
}

以下是将标签结果保存到核心数据的代码:

- (IBAction)editSaveButtonPressed:(id)sender
{

    if (!currentCategory)
        self.currentCategory = (Entity *)[NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:self.managedObjectContext];  

    [self.currentCategory setAttribute:[mlabel text]];  //This is the line causing the error

    NSError *error;

    if (![self.managedObjectContext save:&error])
       NSLog(@"Failed to add new category with error: %@", [error domain]); 

    [self.navigationController popViewControllerAnimated:YES];
}

为什么会导致错误?提前感谢您的意见。

1 个答案:

答案 0 :(得分:1)

看起来attribute上没有定义setAttribute属性(因此Entity)。

您可能希望查看有关核心数据的本教程:http://www.raywenderlich.com/934/core-data-tutorial-getting-started