"IOS" enables automatic serialization of data models

Source: Internet
Author: User

After a period of use, it was found that it was not convenient to upgrade the model when it was changed.

Some time ago, because of the writing of the section of the automatic serialization of code, sub-record:
The principle is that a root class implements the serialization of all data members through a conditioned reflex, and other data models inherit from it.

Decoding implementation

-(ID) Initwithcoder: (Nscoder *) adecoder{self=[Super init];if(self) {IDTempclass = [Selfclass]; while(Tempclass! = [NSObjectclass]) {unsignedintOutcount;ivar* Ivars = Class_copyivarlist (Tempclass, &outcount); for(inti =0; i < Outcount; i++) {//Ivars If it is null Outcount = 0, here is the judgmentConst Char* Typeencode = ivar_gettypeencoding (Ivars[i]);//Types of variablesvoid* Data = (Char*) Self + ivar_getoffset (ivars[i]);//memory address of the variableNSString * name = [NSString stringwithutf8string:ivar_getname (Ivars[i])];//Key Valueif([self issavetodriver:name]) {Switch(typeencode[0]) { Case' I ': Case' I '://integral type with unsigned integer*(int*) data =[Adecoder decodeintforkey:name]; Break; Case' B '://BOOL Type* (BOOL *) data =[Adecoder decodeboolforkey:name]; Break; Case' C '://Character Type*(Char*) data = *[Adecoder decodebytesforkey:name returnedlength:null]; Break; Case' F '://float*(float*) data =[Adecoder decodefloatforkey:name]; Break; Case' d '://Double*(Double*) data =[Adecoder decodedoubleforkey:name]; Break; Case‘@’://Object*(ID*) data =[[Adecoder Decodeobjectforkey:name] retain]; Break; Case‘{‘://Structural Body{Nsuinteger Sizep;ConstUnsignedChar* _data = [Adecoder decodebytesforkey:name returnedlength:&sizep];memcpy (data, _data, Sizep);} Break;default:@throw[NSException exceptionwithname:@"Encoding Exception"Reason:[nsstring stringWithFormat:@"There is an object that is not instantiated, the object type%s, the class%@, and the variable name%@", Typeencode,nsstringfromclass (Tempclass), name] userinfo:nil]; Break;}}//NSLog (@ "Ivar name =%s Type =%s", Ivar_getname (Ivars[i]), ivar_gettypeencoding (Ivars[i]));}tempclass=[Tempclass superclass];free (Ivars);//Release Ivarlist}}returnSelf ;} [/CPP] Code implementation [CPP]-(void) Encodewithcoder: (Nscoder *) acoder{IDTempclass = [Selfclass]; while(Tempclass! = [NSObjectclass]) {unsignedintOutcount;ivar* Ivars = Class_copyivarlist (Tempclass, &outcount);#undefNSLog for(inti =0; i < Outcount; i++) {Const Char* Typeencode = ivar_gettypeencoding (Ivars[i]);//Variable Typevoid* Data = (Char*) Self + ivar_getoffset (ivars[i]);//variable AddressNSString * name = [NSString stringwithutf8string:ivar_getname (Ivars[i])];//Variable name Keyif([self issavetodriver:name]) {Switch(typeencode[0]) { Case' I ': Case' I '://integral type with unsigned integer[Acoder encodeint:*(int*) data forkey:name]; Break; Case' B ': NSLog (@"%x",(int) data); [Acoder Encodebool:* (BOOL *) data forkey:name]; Break; Case' C '://Character Type[Acoder encodebytes:data Length:1Forkey:name]; Break; Case' F '://float[Acoder encodefloat:* (float*) data forkey:name]; Break; Case' d '://Double[Acoder encodedouble:* (Double*) data forkey:name]; Break; Case‘@’://Object[Acoder encodeobject:* (nsobject * *) data forkey:name]; Break; Case‘{‘://Structural Body{Nsuinteger Sizep=0; Nsuinteger Alignment=0; Nsgetsizeandalignment (Typeencode,&sizep, &alignment); [Acoder encodebytes:data Length:sizep forkey:name];} Break;default:{@throw[NSException exceptionwithname:@"Encoding Exception"Reason:[nsstring stringWithFormat:@"there is an object with no encoding, object type%s, class%@, variable name%@", Typeencode,nsstringfromclass (Tempclass), name] userinfo:nil];} Break;}}} Tempclass=[Tempclass superclass];free (Ivars);}}

Copy these two functions to the root class for automatic serialization

Source Blog address, http://onlywish.me

"IOS" enables automatic serialization of data models

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.