coreData了解认识

coreData初步认识

根据这片文档可以自己初步建立一个coreData,进行简单的数据的增删改查。

1、新建工程,记得勾选Use Core Data


coreData了解认识_第1张图片
001.jpeg

2、建立好以后可以看到xxx.xcdatamodeld,在这里可以添加实体和实体的属性。需要注意的是:实体名字必须以大写开头。


coreData了解认识_第2张图片
002.jpeg

3、然后新建一个file,记得是NSManagedObject cubclass
coreData了解认识_第3张图片
003.jpeg

4、勾选自己建立的工程


coreData了解认识_第4张图片
004.jpeg

5、勾选建立的实体
coreData了解认识_第5张图片
005.jpeg

6、next以后我们就可以看到建立好的实体是有4个文件,如图一

这里需要注意的是,xcode7以后建立的都是4个,而7以前的是两个,如图二

解释如下:So as you can see now all properties are in a separate file with category (CoreDataProperties). Later if you generate NSManagedObject subclass for the same model Xcode 7 will regenarete only 2 files with category (DBUser+CoreDataProperties.h and DBUser+CoreDataProperties.m) to update all properties from your model but it will not make any changes to 2 other files (DBUser.h and DBUser.m) so you can use these 2 files to add there some custom methods or properties etc.

In previous version Xcode generated always only 2 files (DBUser.h and DBUser.m) and it put properties there so you could not easily modify these files because your custom implementation was deleted everytime you regenerated your subclasses. Therefore it was a common practice to manually create a category and put your methods in your category which was oposite to what we can see in Xcode 7. That however had many disadvantages because we had to use a category for implementation of our methods which does not allow to do certain things and now we can easily modify the main interface and implementation files which allows us to do anything with it. Hurray!


006.jpeg

图一


coreData了解认识_第6张图片
006.1.jpeg

7、
coreData了解认识_第7张图片
007.jpeg

coreData了解认识_第8张图片
007.2.jpeg

8、增删改查(其实顺序应该是增、查、删或者改)


coreData了解认识_第9张图片
008.1.jpeg

coreData了解认识_第10张图片
008.2.jpeg

查询结果


coreData了解认识_第11张图片
008.3.jpeg

coreData了解认识_第12张图片
008.4.png

删除后再进行查,查询结果:
coreData了解认识_第13张图片
008.5.jpeg

coreData了解认识_第14张图片
008.6.png

改完后的结果:
coreData了解认识_第15张图片
008.7.jpeg

至此,完成。

这里知识coredata的初步认识,具体的增删改查还需要和自己的项目结合。

你可能感兴趣的:(coreData了解认识)