On Weibo two days ago, I saw Liu Xin and others DISCUSSING THE ORM stored in databases on Android and IOs (Object relationship ing, that is, ing databases to objects. Most of the data to be stored in your project is relatively simple, so no ORM is used for Android or IOS. In fact, a major feature of core data is the processing of ORM.
We first design our database in the xcdatamodel file: for example, if I create a data object, there is a string type attribute name and an integer type num:
Select data, add a file, and select the nsmanagedobject subclass type:
Xcode will automatically map objects to our database and generate the data. h Data. M file. Data is a subclass derived from nsmanagedobject,
# Import<Foundation/Foundation. h># Import<Coredata/coredata. h>@ InterfaceData: nsmanagedobject @ property (nonatomic, retain) nsstring*Name; @ property (nonatomic, retain) nsnumber*Num;@ End
# Import "Data. h"@ ImplementationData @ dynamic name; @ dynamic num;@ End
We can directly use the data object where we need to store coredata. Of course, you need to import data. h first, for example, my demoCode:
-( Void ) Savedatawithname :( nsstring *) Name andnum :( Int ) Num {appdelegate * Appdelegate = [[uiapplication sharedapplication] Delegate ]; Nsmanagedobjectcontext * Context = Appdelegate. managedobjectcontext; // Save data Data * Data = [nsentitydescription insertnewobjectforentityforname: @" Data " Inmanagedobjectcontext: Context]; data. Name = Name; data. Num = [Nsnumber numberwithint: num]; [appdelegate savecontext]; // Get Data Nserror * Error; nsfetchrequest * Request = [[Nsfetchrequest alloc] init]; nsentitydescription * Entity = [nsentitydescription entityforname: @" Data " Inmanagedobjectcontext: Context]; [Request setentity: entity]; nsarray * Results = [[context executefetchrequest: Request error :& Error] Copy]; For (Data * d In Results) {nslog ( @" > Date. Name: % @ data. Num: % I " , D. Name, [D. Num intvalue]) ;}}
In fact, the previous practice (Data Storage in iPhone development: core data) is to directly use the nsmanagedobject object for access. Now, it is mapped to the corresponding object. Of course, when the database is complex, its benefits will be reflected.
These are simple ORM in IOS.
Reference: http://marshal.easymorse.com/archives/3377