coreData初步認(rèn)識(shí)

coreData初步認(rèn)識(shí)

根據(jù)這片文檔可以自己初步建立一個(gè)coreData,進(jìn)行簡(jiǎn)單的數(shù)據(jù)的增刪改查。

1、新建工程,記得勾選Use Core Data

2、建立好以后可以看到xxx.xcdatamodeld,在這里可以添加實(shí)體和實(shí)體的屬性。需要注意的是:實(shí)體名字必須以大寫開頭。

3、然后新建一個(gè)file,記得是NSManagedObject cubclass(但是在Xcode8中,不用了這一步了)

4、勾選自己建立的工程

5、勾選建立的實(shí)體

6、next以后我們就可以看到建立好的實(shí)體是有4個(gè)文件,如圖一

這里需要注意的是,xcode7以后建立的都是4個(gè),而7以前的是兩個(gè),如圖二

解釋如下: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!

圖一

操作過程


7、增刪改查(其實(shí)順序應(yīng)該是增、查、刪或者改)

最新的寫法:Dog * dog = [[Dog alloc]initWithEntity: entityDescription insertIntoManagedObjectContext:self.appDelegate.persistentContainer.viewContext];

查詢結(jié)果

刪除后再進(jìn)行查,查詢結(jié)果:

改完后的結(jié)果:

至此,完成。

這里知識(shí)coredata的初步認(rèn)識(shí),具體的增刪改查還需要和自己的項(xiàng)目結(jié)合。

Demo下載:https://pan.baidu.com/s/1milyrlI

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容