Ios-mjextension a word. Object archive

Source: Internet
Author: User

Mjextension is powerful and almost supports all existing models, dictionaries, JSON data transformations, and is highly efficient

when writing objects in the past, you need to get this object to implement the Nscoding protocol , and to do some encode and decode operations on each attribute that needs to be archived, such as your previous code might be.

Person.h file

#import "Person.h"@interface Person : NSObject <NSCoding>@property (nonatomicNSString *name;@property (nonatomic,assignint age;@property (nonatomic,assignBOOL gay;@end

PERSON.M file

#import "Person.h"  @implementation   person-(ID) Initwithcoder: (Nscoder *) adecoder{if( Self= [SuperInit]) { Self. Name= [Adecoder decodeobjectforkey:@"Name"];NSNumber*at = [Adecoder decodeobjectforkey:@"Age"]; Self. Age= at. Intvalue;NSNumber*isgay = [Adecoder decodeobjectforkey:@"Gay"]; Self. Gay= Isgay. Intvalue; }return  Self;}/** * How to store an object when writing to a file * How to save * * @param acoder acoder */-(void) Encodewithcoder: (Nscoder *) acoder{[Acoder encodeobject: Self. Nameforkey:@"Name"]; [Acoder encodeobject:[NSNumberNumberwithint: Self. Age] forkey:@"Age"]; [Acoder encodeobject:[NSNumberNumberwithint: Self. Gay] forkey:@"Gay"];}@end

this needs to rewrite Initwithcoder and Encodewithcoder These two methods, more cumbersome, this and Android inside the implementation parcelable almost, to write a lot of code, but Android has a solution to this problem, By installing the Androidstudio plug-in to solve, see the previous androidstudio-parcelable automatically generated code plug-in installation and use of this article, in fact, iOS also has a relevant solution, and more than the Android implementation is simpler, Fast and efficient, a code to fix, don't believe? I didn't believe it before, but when I saw Mjextension, I believed it!

Mjextension very useful but easy to ignore features: No matter your model attributes have hundreds of, only need to add a macro mjcodingimplementation, you can implement archive files, no longer write disgusting encodewithcoder: and Initwithcoder: the

The code example below, Person.h code is not changed, just need to change the contents of the Person.m file to the following is OK:

#import "Person.h"#import "MJExtension.h"@implementation PersonMJCodingImplementation@end

Fix it, what is it, so it's done? Not deceptive, certainly not, in fact, archive and anti-archive code is essentially written, but mjextension help us do, we go to click on this mjcodingimplementation to see, found that the code is this:

#import <Foundation/Foundation.h>@interface NSObject (MJCoding)/** *  解码(从文件中解析对象) */- (void)decode:(NSCoder *)decoder;/** *  编码(将对象写入文件中) */- (void)encode:(NSCoder *)encoder;@end/** 归档的实现 */if (self = [superreturn self; } - (void)encodeWithCoder:(NSCoder *)encoder { [self encode:encoder]; }

And look at the implementation class code

#Import "Nsobject+mjcoding.h"#Import "Nsobject+mjmember.h"@implementationNSObject (mjcoding)/** * Encoding (writing an object to a file) */- (void) Encode: (Nscoder *) encoder{[self enumerateivarswithblock:^ (Mjivar *ivar, BOOL *stop) {if(ivar.issrcclassfromfoundation)return;    [Encoder EncodeObject:ivar.value ForKey:ivar.name]; }];}/** * Decoding (parsing objects from a file) */- (void) Decode: (Nscoder *) decoder{[self enumerateivarswithblock:^ (Mjivar *ivar, BOOL *stop) {if(ivar.issrcclassfromfoundation)return;    Ivar.value = [decoder decodeObjectForKey:ivar.name]; }];}@end

It actually helped us to get all the properties archived and archived, so the problem is, I don't want all the files to be archived, I just want to archive the name and age attributes and don't want to archive the gay attribute. In fact, Mjextension has helped us to think about it, just need to call:

[Person setupIgnoredCodingPropertyNames:^NSArray *{    return @[@"gay"];}];

That's the way it's going to be!!!!!!!!

Finally, attach Mjextension's github address.

Ios-mjextension a word. Object archive

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.