coreData初步認識

coreData初步認識

根據這片文檔可以自己初步建立一個coreData,進行簡單的數據的增刪改查。

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



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


3、然後新建一個file,記得是NSManagedObject cubclass


4、勾選自己建立的工程


5、勾選建立的實體


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!


圖一




圖二

7、



8、增刪改查(其實順序應該是增、查、刪或者改)



查詢結果



刪除後再進行查,查詢結果:





改完後的結果:



至此,完成。

這裏知識coredata的初步認識,具體的增刪改查還需要和自己的項目結合。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章