First step: Add a third-party library, add a frame
Step Two: Create a data model and create an entity
Step three: #import "coredata+magicalrecord.h"//the header file must be added to the. pch file
Fourth Step:
Initialize the database information, write the name as needed
[Magicalrecord setupcoredatastackwithstorenamed:@ "model.db"];
Main operation
-(void) ButtonClick: (ID) sender{UIButton* Button = (UIButton *) sender; Switch(Button.tag) { Case 1: { //Increase//<1> Creating ObjectsStudent * Stu =[Student mr_createentity]; //<2> assigning values to member properties of an objectStu.firstname =@"Lele"; Stu.lastname=@"du"; Stu.age=@" -"; //<3> adding objects to the database//Mr_defaultcontext Creating a Context object//mr_saveonlyselfandwait to write the modified results back to the database[[Nsmanagedobjectcontext Mr_defaultcontext] mr_saveonlyselfandwait]; } Break; Case 2: { //DeleteStudent * Stu =[Student Mr_findfirst]; if(Stu) {[Stu mr_deleteentity];//remove an object from memory } //To delete an object from the database[[Nsmanagedobjectcontext Mr_defaultcontext] mr_savetopersistentstoreandwait]; } Break; Case 3: { //ChangeNsarray * array = [Student mr_findbyattribute:@" Age"Withvalue:@" -"]; for(Student * STRinchArray) {Str.lastname=@"Hello"; Str.firstname=@" China"; Str.age=@"Lele"; } [[Nsmanagedobjectcontext Mr_defaultcontext] mr_savetopersistentstoreandwait]; } Break; default: { //Check//<1> Search by CriteriaNsarray * array = [Student mr_findbyattribute:@" Age"Withvalue:@" -"]; for(Student * STRinchArray) {NSLog (@"%@", Str.lastname); } //<2> querying all data in the databaseNsarray * Array1 =[Student Mr_findall]; for(Student * STRincharray1) {NSLog (@"%@", Str.lastname); } //<3> Querying the first data information in a databaseStudent * Stu =[Student Mr_findfirst]; NSLog (@"%@", Stu.firstname); } Break; }}
CoreData third-party library Magicalrecord