coreData初步认识
根据这片文档可以自己初步建立一个coreData,进行简单的数据的增删改查。
1、新建工程,记得勾选Use Core Data
![](https://i-blog.csdnimg.cn/blog_migrate/99f07a97367366fbeff74277dc75fce4.webp?x-image-process=image/format,png)
2、建立好以后可以看到xxx.xcdatamodeld,在这里可以添加实体和实体的属性。需要注意的是:实体名字必须以大写开头。
![](https://i-blog.csdnimg.cn/blog_migrate/a11eecd8f3999f05481c7d81779a0c1d.webp?x-image-process=image/format,png)
3、然后新建一个file,记得是NSManagedObject cubclass
![](https://i-blog.csdnimg.cn/blog_migrate/daf672a3e4235ab5c0b3e85cbd103cd0.webp?x-image-process=image/format,png)
4、勾选自己建立的工程
![](https://i-blog.csdnimg.cn/blog_migrate/d16bbe2cd796f2c8177f778e6e1a958f.webp?x-image-process=image/format,png)
5、勾选建立的实体
![](https://i-blog.csdnimg.cn/blog_migrate/15782381f92211b4ba55d636aff24d4a.webp?x-image-process=image/format,png)
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!
![](https://i-blog.csdnimg.cn/blog_migrate/6ac78ce84e2e4ead2a1f4234f2e5bd2b.webp?x-image-process=image/format,png)
图一
![](https://i-blog.csdnimg.cn/blog_migrate/cccdf33201ea2f20cbabdb11e6121814.webp?x-image-process=image/format,png)
7、
![](https://i-blog.csdnimg.cn/blog_migrate/935839a703ece65bd58bbc8672d2eca5.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/695acc2495072726adf1dbfaf8fcb789.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/d735ef3c9722e0517b5decf43457cb89.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/4d80f10d400c8622dccaf5ee5ff10ea0.webp?x-image-process=image/format,png)
查询结果
![](https://i-blog.csdnimg.cn/blog_migrate/ee99c7680306bceecf7ae18313361e05.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/fec209c4ce30e68d330be104fde41613.webp?x-image-process=image/format,png)
删除后再进行查,查询结果:
![](https://i-blog.csdnimg.cn/blog_migrate/f212046f46a7347e70e9e27600735d2d.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/dc796dfa69d8eeef5849e9a4df0d2606.webp?x-image-process=image/format,png)
改完后的结果:
![](https://i-blog.csdnimg.cn/blog_migrate/3796b5eb83fba5c5b622f73e814c9c9f.webp?x-image-process=image/format,png)
至此,完成。
这里知识coredata的初步认识,具体的增删改查还需要和自己的项目结合。
原文链接:http://www.jianshu.com/p/337872d95727
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。