核心数据读取数据

核心数据读取数据

本文介绍了核心数据读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Core Data的新手,我最近一直在尝试读写数据。我用实体名称和年龄创建了一个名为人的实体。我也有一个文本字段名称 personName和一个文本字段名为 personAge。

I am a VERY new beginner to Core Data and I have recently been trying to read and write data. I created an entity named "Person" with the entities "name" and "age". I also have a textfield name "personName" and a textfield named "personAge".

- (IBAction)readData:(id)sender
{
    NSNumber *ageNum = [NSNumber numberWithInteger:personAge.text.integerValue];
    Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:managedObjectContext];

    newPerson.name = personName.text;
    newPerson.age = ageNum;

    NSLog(@"Person %@ name is %@", personName.text, ageNum);
}

加载应用程序时,我得到的只是SIGABRT。即使我在方法中输入的全部内容是

When I load the app, all i get is SIGABRT. Even when all I put in the method is

 Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person"     inManagedObjectContext:managedObjectContext];

感谢所有帮助。

推荐答案

对于将值添加到核心数据,您可以这样做:-

For Adding values to the core data you can do so:-

- (IBAction)save:(id)sender {
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new managed object
    NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
    [person setValue:self.personName.text forKey:@"name"];
    [person setValue:self.personAge.text forKey:@"age"];

    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
}

用于从核心数据中获取值:-

For fetching the values from core data:-

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
    self.personValues = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

此处personValues是一个NSMutableArray。
要进一步了解这些内容,请访问此链接。

here personValues is a NSMutableArray.For better understanding about these please go through this link.

希望这会帮助您。

这篇关于核心数据读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:44