[iOS]Core Data浅析(二)

来源:互联网 发布:软件英语怎么说 编辑:程序博客网 时间:2024/05/16 06:54

在上篇文章中,笔者写了如何创建Core Data项目,以及创建数据模型,没有阅读的小伙伴请移步,先了解一下相关内容吧[iOS]Core Data浅析(一);

今天,我们来讨论,如何将创建的可视化模型,转化为OC的对象模型.

接着我们上一个工程继续操作;

一. 转化为对象模型

来到我们的LZCoreData.xcdatamodeld文件,选中PeopleEntity实体,然后点击Xcode导航栏的File-->New -->File...或者直接command+N,新建文件,选择Core Data-->NSManagedObject subclass



Next-->Next.来到如下界面:



这里会显示项目中所有待转化为模型的实体列表,选择你要转化的实体,继续Next-->Create;

这时,项目的左侧导航会多了几个文件:



就是分别为PeopleEntit和ManEntity创建的模型文件,这里我们主要用到的是那四个名称比较长的类目文件;

这里有一个问题,需要我们手动处理一下:

比较这两个类目文件,我们会发现,在PeopleEntity+CoreDataProperties.h文件中的manRelationship属性被声明为了NSManagedObject类型,而我们创建的模型中这个属性类型是ManEntity,这是不对的;而在ManEntity+CoreDataProperties.h文件中的peopleRelationship是PeopleEntity类型的,是正确的;这是因为在生成PeopleEntity的模型的时候ManEntity的模型并不存在,所以系统不知道有这个类型,就直接声明为NSManagedObject类型;而在生成ManEntity的模型的时候,PeopleEntity的模型已经存在了,所以他的属性声明是正常的;

这时,只需要我们手动改一下PeopleEntity+CoreDataProperties.h的属性manRelationship的类型为ManEntity即可;

改完后,一编译发现报错了,这是因为缺少对ManEntity的引用,这里我们并不需要直接访问ManEntity的属性,所以只需要在PeopleEntity.h中加上:

@class ManEntity;

再编译,就没有问题了;


二. 使用对象模型

模型转化完成,就可以在我们的代码中使用了,来到我们的ViewController.m文件,导入头文件PeopleEntity+CoreDataProperties.h和ManEntity+CoreDataProperties.h,修改为以下代码:

//获取代理    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];    //获取context    NSManagedObjectContext *context = [delegate managedObjectContext];        //获取PeopleEntity实体    //这里修改为PeopleEntity类型    PeopleEntity *people = [NSEntityDescription insertNewObjectForEntityForName:@"PeopleEntity" inManagedObjectContext:context];        //设置属性内容    people.name = @"流火绯瞳";    people.age = @27;    people.sex = @0;//    [people setValue:@"流火绯瞳" forKey:@"name"];//    [people setValue:@26 forKey:@"age"];//    [people setValue:@0 forKey:@"sex"];        //获取ManEntit实体    //这里修改为ManEntity类型    ManEntity *man = [NSEntityDescription insertNewObjectForEntityForName:@"ManEntity" inManagedObjectContext:context];        man.height = @178.0;    man.weight = @60.0;    man.name = @"张三丰";    man.peopleRelationship = people;    //    [man setValue:@178.0 forKey:@"height"];//    [man setValue:@60.0 forKey:@"weight"];//    [man setValue:@"张三" forKey:@"name"];//    [man setValue:people forKey:@"peopleRelationship"];//    //    [people setValue:man forKey:@"manRelationship"];        people.manRelationship = man;        NSError *error;    //保存更改    if ([context save:&error]) {        NSLog(@"保存成功");    } else {        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);    }        //查询实体    //创建一个查询请求    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];    //获取要查询的实体    NSEntityDescription *entity = [NSEntityDescription                                   entityForName:@"PeopleEntity" inManagedObjectContext:context];    //添加到查询请求    [fetchRequest setEntity:entity];    //开始查询并获取结果    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];        NSLog(@"输出查询结果");    for (PeopleEntity *info in fetchedObjects) {                NSLog(@"Name: %@", info.name);        NSLog(@"age: %@", info.age);        NSLog(@"sex: %@", info.sex);        NSLog(@"-----------------------------------");        //        NSLog(@"Name: %@", [info valueForKey:@"name"]);//        NSLog(@"age: %@", [info valueForKey:@"age"]);//        NSLog(@"sex: %@", [info valueForKey:@"sex"]);//        NSLog(@"-----------------------------------");                ManEntity *man1 = [info valueForKey:@"manRelationship"];                        NSLog(@"Name: %@", man1.name);        NSLog(@"weight: %@", man1.weight);        NSLog(@"height: %@", man1.height);        NSLog(@"==========================================");//        NSLog(@"Name: %@", [man1 valueForKey:@"name"]);//        NSLog(@"weight: %@", [man1 valueForKey:@"weight"]);//        NSLog(@"height: %@", [man1 valueForKey:@"height"]);//        NSLog(@"==========================================");    }

为了便于比较前后做了哪些更改,这里我没有把原来的KVC模式赋值取值方法删除,只是注释掉了;并添加了我们熟悉的对象属性赋值取值方式;

运行一下看看结果吧:

2016-05-26 16:22:27.286 LZCoreData[5765:594436] 保存成功2016-05-26 16:22:27.288 LZCoreData[5765:594436] 输出查询结果2016-05-26 16:22:27.288 LZCoreData[5765:594436] Name: 流火绯瞳2016-05-26 16:22:27.288 LZCoreData[5765:594436] age: 262016-05-26 16:22:27.288 LZCoreData[5765:594436] sex: 02016-05-26 16:22:27.288 LZCoreData[5765:594436] -----------------------------------2016-05-26 16:22:27.289 LZCoreData[5765:594436] Name: 张三2016-05-26 16:22:27.289 LZCoreData[5765:594436] weight: 602016-05-26 16:22:27.289 LZCoreData[5765:594436] height: 1782016-05-26 16:22:27.290 LZCoreData[5765:594436] ==========================================2016-05-26 16:22:27.290 LZCoreData[5765:594436] Name: 流火绯瞳2016-05-26 16:22:27.290 LZCoreData[5765:594436] age: 272016-05-26 16:22:27.290 LZCoreData[5765:594436] sex: 02016-05-26 16:22:27.290 LZCoreData[5765:594436] -----------------------------------2016-05-26 16:22:27.290 LZCoreData[5765:594436] Name: 张三丰2016-05-26 16:22:27.290 LZCoreData[5765:594436] weight: 602016-05-26 16:22:27.290 LZCoreData[5765:594436] height: 1782016-05-26 16:22:27.308 LZCoreData[5765:594436] ==========================================

什么?有两组数据!!  

如果你是接着上篇文章做的,这里当然会输出两组数据,因为我们保存了两次数据嘛.

由可视化的模型转化为对象模型到这里就完成了,然后就可以在项目中使用了.

当然,到目前为止,我们只是介绍了Core Data的使用准备工作,真正使用的话,我们需要完善他的增删改查功能;这里,推荐一个已经写得不错的文章IOS之分析网易新闻存储数据(CoreData的使用,增删改查).

0 0