Copy of IOS custom object and subclass and model set model, common code for archival storage

Source: Internet
Author: User

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>

#pragma
Mark-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.