iOS quickly archive files with Objc/runtime and kvc--

Source: Internet
Author: User

Sometimes it is necessary to store some lightweight data during the development process, and there are several data storage methods available for iOS that are most appropriate for the object archive: nscoding

But two methods need to be implemented to archive the object data: Encodewithcoder and Initwithencoder. Encodewithcoder is coding, Initwithcoder is decoding.

The Encodewithcoder method passes in a Nscoder object, and when implemented we can invoke various methods such as Encodeobject, Encodefloat, and Encodeint and encode by specifying the key value, and

call Decodedoubleforkey,decodefloatforkey,decodeobjectforkey when needed to decode.

1. Implementing Encodewithcoder and Initwithencoder

#import "MyModel.h"@implementationMyModel#defineKcustidkey @ "Custidkey"#defineKcuststatuskey @ "Custstatuskey"#defineKpictureidkey @ "Pictureidkey"#defineKunitnumkey @ "Unitnumkey"#defineKcityidkey @ "Cityidkey"#defineKcustnamekey @ "Custnamekey"#defineKcommunitynamekey @ "Communitynamekey"#defineKreserve1key @ "Reserve1key"#defineKcitykey @ "Citykey"#defineKcommunityidkey @ "Communityidkey"#defineKcitynamekey @ "Citynamekey"#defineKbuildingnumkey @ "Buildingnumkey"#defineKnicknamekey @ "Nicknamekey"#defineKhousenumkey @ "Housenumkey"#defineKphonekey @ "Phonekey"//Archive- (void) Encodewithcoder: (Nscoder *) acoder{[Acoder encodeobject:self.    CUSTID Forkey:kcustidkey]; [Acoder encodeobject:self.    Custstatus Forkey:kcuststatuskey]; [Acoder encodeobject:self.    Pictureid Forkey:kpictureidkey]; [Acoder encodeobject:self.    Unitnum Forkey:kunitnumkey]; [Acoder encodeobject:self.    PHONE Forkey:kphonekey]; [Acoder encodeobject:self.    Cityid Forkey:kcityidkey]; [Acoder encodeobject:self.    CustName Forkey:kcustnamekey];    [Acoder EncodeObject:self.COMMUNITYNAME Forkey:kcommunitynamekey]; [Acoder encodeobject:self.    RESERVE1 Forkey:kreserve1key]; [Acoder encodeobject:self.    City Forkey:kcitykey];    [Acoder EncodeObject:self.COMMUNITYID Forkey:kcommunityidkey]; [Acoder encodeobject:self.    CityName Forkey:kcitynamekey]; [Acoder encodeobject:self.    Buildingnum Forkey:kbuildingnumkey]; [Acoder encodeobject:self.    Nickname Forkey:knicknamekey]; [Acoder encodeobject:self. HouseNum Forkey:khousenumkey];} //solution file- (ID) Initwithcoder: (Nscoder *) decoder{if(self =[Super Init]) {self. CUSTID=[Decoder Decodeobjectforkey:kcustidkey]; Self. Custstatus=[Decoder Decodeobjectforkey:kcuststatuskey]; Self. Pictureid=[Decoder Decodeobjectforkey:kpictureidkey]; Self. PHONE=[Decoder Decodeobjectforkey:kphonekey]; Self. Unitnum=[Decoder Decodeobjectforkey:kunitnumkey]; Self. Cityid=[Decoder Decodeobjectforkey:kcityidkey]; Self. CustName=[Decoder Decodeobjectforkey:kcustnamekey]; Self.communityname=[Decoder Decodeobjectforkey:kcommunitynamekey]; Self. RESERVE1=[Decoder Decodeobjectforkey:kreserve1key]; Self. City=[Decoder Decodeobjectforkey:kcitykey]; Self.communityid=[Decoder Decodeobjectforkey:kcommunityidkey]; Self. CityName=[Decoder Decodeobjectforkey:kcitynamekey]; Self. Buildingnum=[Decoder Decodeobjectforkey:kbuildingnumkey]; Self. Nickname=[Decoder Decodeobjectforkey:knicknamekey]; Self. HouseNum=[Decoder Decodeobjectforkey:khousenumkey]; }    returnSelf ;}@end

  After writing, found that the whole person is not good, if the object properties more, these repetitive code also means to be command+c command+v n times, tired feel no love.

Then try to do it in another way, since the Objective-c Runtime library provides a convenient way to get the class and all its member variables that its object is running, and to access the values through KVC, or you can OBJC/RUNTIME+KVC

// get all Properties    in a class int count;     class], &count);      for int i=0; i<count; i++) {        = vara[i];         Const char *name = ivar_getname (v);        NSLog (@ "%s==", name);    }

  Print log:

    

Good, continue, take advantage of KVC to get the value, according to the property name to obtain the corresponding value

// KVC Value        ID value = [self valueforkey:strname];        [Encoder encodeobject:value Forkey:strname];

After the final change of the program:

#import "MyModel.h"#import<objc/runtime.h>@implementationMyModel//solution file- (ID) Initwithcoder: (Nscoder *) decoder{if(self =[Super Init]) {unsignedintCount =0; //gets the name of all member variables in the classIvar *ivar = Class_copyivarlist ([MyModelclass], &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); }    returnSelf ;}//Archive- (void) Encodewithcoder: (Nscoder *) encoder{unsignedintcount; Ivar*ivar = Class_copyivarlist ([MyModelclass], &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);}@end

Properly, away from repetition, do fun and play the program of the daughter!

iOS quickly archive files with Objc/runtime and kvc--

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.