IOS uses objc/runtime and KVC to quickly archive and archive files and objckvc

Source: Internet
Author: User

IOS uses objc/runtime and KVC to quickly archive and archive files and objckvc

Sometimes some lightweight data needs to be stored during the development process. For several data storage methods provided by IOS, it is most suitable for object Archiving: NSCoding

However, two methods are required to archive and archive object data: encodeWithCoder and initWithEncoder. EncodeWithCoder is the encoding, and initWithCoder is the decoding.

The encodeWithCoder method imports an NSCoder object. When implemented, we can call various methods such as encodeObject, encodeFloat, and encodeInt and encode them by specifying key values.

Call decodeDoubleForKey, decodeFloatForKey, and decodeObjectForKey for decoding as needed.

1. encodeWithCoder and initWithEncoder

# Import "MyModel. h "@ implementation MyModel # define keys @" CUSTIDKey "# define keys @" CUSTSTATUSKey "# define keys @" PICTUREIDKey "# define kUNITNUMKey @" UNITNUMKey "# define keys @" CITYIDKey" # define attributes @ "CUSTNAMEKey" # define kCOMMUNITYNAMEKey @ "COMMUNITYNAMEKey" # define attributes @ "attributes" # define kCITYKey @ "CITYKey" # define kCOMMUNITYIDKey @ "COMMUNITYIDKey" # define attributes @" CITYNAMEKey "# define kBUILDINGNUMKey @" BUILDINGNUMKey "# define kNICKNAMEKey @" NICKNAMEKey "# define HOUSENUMKey @" HOUSENUMKey "# define kPHONEKey @" PHONEKey "/archive-(void) encodeWithCoder :( NSCoder *) acder {[acder 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]; [ecoder encodeObject: self. COMMUNITYID forKey: kCOMMUNITYIDKey]; [aCoder encodeObject: self. CITYNAME forKey: kCITYNAMEKey]; [aCoder encodeObject: self. BUILDINGNUM forKey: kBUILDINGNUMKey]; [aCoder encodeObject: self. NICKNAME forKey: kNICKNAMEKey]; [ecoder encodeObject: self. HOUSENUM forKey: housenumkey];} // unarchive-(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];} return self;} @ end

After writing the code, I found that the whole person is not good. If there are more object attributes, these repetitive Code also means that the Code will be executed N times by Command + C Command + V, so I feel bored.

Then try other methods. Since the Objective-C Runtime Library provides a very convenient way to get the class of its object runtime and all its member variables, and access the value through KVC, in this case, objc/runtime + KVC

Import # import <objc/runtime. h> header file

// Obtain all the attributes of the class: unsigned int count; Ivar * varA = class_copyIvarList ([MyModel class], & count); for (unsigned int I = 0; I <count; I ++) {Ivar v = varA [I]; const char * name = ivar_getName (v); NSLog (@ "% s =", name );}

Print LOG:

    

Easy to use. Continue. Use KVC to obtain the corresponding value based on the attribute name.

// KVC value id value = [self valueForKey: strName]; [encoder encodeObject: value forKey: strName];

The final modified program:

# Import "MyModel. h "# import <objc/runtime. h> @ implementation MyModel // archive-(id) initWithCoder :( NSCoder *) decoder {if (self = [super init]) {unsigned int count = 0; // obtain the names of all member variables in the class Ivar * ivar = class_copyIvarList ([MyModel class], & count); for (int I = 0; I <count; I ++) {Ivar iva = ivar [I]; const char * name = ivar_getName (iva); NSString * strName = [NSString stringwithuf8string: name]; // obtain the value id value = [decoder decodeObjectForKey: strName]; // assign a value to the attribute using KVC [self setValue: value forKey: strName];} free (ivar );} return self;} // archive-(void) encodeWithCoder :( NSCoder *) encoder {unsigned int count; Ivar * ivar = class_copyIvarList ([MyModel class], & count ); for (int I = 0; I <count; I ++) {Ivar iv = ivar [I]; const char * name = ivar_getName (iv ); NSString * strName = [NSString stringwithuf8string: name]; // use the KVC value id value = [self valueForKey: strName]; [encoder encodeObject: value forKey: strName];} free (ivar) ;}@ end

It's a good deal. Keep yourself away from repetition and have fun!

 

Related Article

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.