First, runtime implementation of general copy
If the subclass of the custom class, model set model do you really copy it, be careful to have a pit.
Copy requires custom class inheritance Nscopying protocol
#import<objc/runtime.h>-(ID) Copywithzone: (Nszone *) Zone {IDobj = [[Selfclass] [allocwithzone:zone] init]; Classclass= [Selfclass]; while(class! = [NSObjectclass]) {unsignedintcount; Ivar*ivar = Class_copyivarlist (class, &count); for(inti =0; I < count; i++) {Ivar IV=Ivar[i]; Const Char*name =Ivar_getname (iv); NSString*strname =[NSString Stringwithutf8string:name]; //using KVC to take a value IDValue = [[self valueforkey:strname] copy];//If we have a model, copy it, too .[obj setvalue:value forkey:strname]; } free (Ivar); class= Class_getsuperclass (class);//Remember to traverse the properties of the parent class as well . } returnobj;}Second, runtime implementation of universal archive solution
Archive files require custom class inheritance nscoding protocol
#import <objc/runtime.h>
#pragmaMark-Archive, Solution-(void) Encodewithcoder: (Nscoder *) Encoder {Classclass= [Selfclass]; while(class! = [NSObjectclass]) {unsignedintcount; Ivar*ivar = Class_copyivarlist (class, &count); for(inti =0; I < count; i++) {Ivar IV=Ivar[i]; Const Char*name =Ivar_getname (iv); NSString*strname =[NSString Stringwithutf8string:name]; //using KVC to take a value IDValue =[self valueforkey:strname]; [Encoder Encodeobject:value Forkey:strname]; } free (Ivar); class= Class_getsuperclass (class);//Remember to traverse the properties of the parent class as well . }}- (ID) Initwithcoder: (Nscoder *) Decoder { Self=[Super Init]; if(self) {Classclass= [Selfclass]; while(class! = [NSObjectclass]) {unsignedintCount =0; //gets the name of all member variables in the classIvar *ivar = Class_copyivarlist (class, &count); for(inti =0; I < count; i++) {Ivar IVA=Ivar[i]; Const Char*name =Ivar_getname (IVA); NSString*strname =[NSString Stringwithutf8string:name]; //to extract the value of the file IDValue =[Decoder decodeobjectforkey:strname]; //using KVC to assign a value to a property[self setvalue:value forkey:strname]; } free (Ivar); class= Class_getsuperclass (class);//Remember to traverse the properties of the parent class as well . } } returnSelf ;}
Iii. Nsuserdefaults Store Custom objects
//1. The custom class inherits the Nscoding protocol and implements the archive file, which is described earlier, not much said//2. Turn the custom object to NSDataNSData *data =[Nskeyedarchiver archiveddatawithrootobject:student];//3. Save to Nsuserdefaults[[Nsuserdefaults Standarduserdefaults] Setobject:data forkey:@"mystudent"]; [[Nsuserdefaults standarduserdefaults] synchronize];//A lot of people forget about this sentence.//4. Save an array with the custom object in the array[DataArray Addobject:data];//This data is the 2nd step to archive the data//5.NSUserDefaults can only save immutable groups, do not forget to convert to immutableNsarray * Array =[Nsarray Arraywitharray:dataarray]; [[Nsuserdefaults Standarduserdefaults] Setobject:array forkey:@"Students"];
Iv. read archived custom objects in Nsuserdefaults
NSData *data = [[Nsuserdefaults standarduserdefaults] Objectforkey:@ "mystudent" *student = [Nskeyedunarchiver unarchiveobjectwithdata:data];
Copy of IOS custom object and subclass and model set model, common code for archival storage