MJExtension很强大,几乎支持现有所有的模型、字典、json数据转换,而且效率非常高
以前写对象归档的时候,需要让这个对象实现NSCoding协议,而且要对每一个需要归档的属性做一些encode和decode操作,例如你之前的代码可能是这样子的
Person.h文件
#import "Person.h"
@interface Person : NSObject <NSCoding>
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) int age;
@property (nonatomic,assign) BOOL gay;
@end
Person.m文件
#import "Person.h"
@implementation Person
-(id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
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;
}
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:[NSNumber numberWithInt:self.age] forKey:@"age"];
[aCoder encodeObject:[NSNumber numberWithInt:self.gay] forKey:@"gay"];
}
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
这需要重写initWithCoder和encodeWithCoder这两个方法,比较繁琐,这和Android里面的实现Parcelable差不多,要写一大堆的代码,不过Android有解决这个问题的方案,通过安装AndroidStudio插件来解决,请看前面讲的AndroidStudio-Parcelable自动生成代码插件安装和使用这篇文章,其实,IOS里面也有相关的解决方案,而且比Android实现更简单,快速和高效,一句代码搞定,不信?之前我也不信,但是当我看到MJExtension之后,我就信了!
MJExtension非常好用但是容易被忽略的功能:不管你的模型属性有几百个,只需要加一句宏MJCodingImplementation,就能实现归档解档,不用再编写恶心的encodeWithCoder:和initWithCoder:了
代码示例如下,Person.h代码不变,只需要将Person.m文件内容改成下面的就OK了:
#import "Person.h"
#import "MJExtension.h"
@implementation Person
MJCodingImplementation
@end
搞定,啥玩意,这样写就实现了?不是坑人的吧,肯定不是喽,其实归档和反归档代码本质上都要写,只不过MJExtension帮我们做了,我们去点击这个MJCodingImplementation去看看,发现代码是这样的:
#import <Foundation/Foundation.h>
@interface NSObject (MJCoding)
/**
* 解码(从文件中解析对象)
*/
- (void)decode:(NSCoder *)decoder;
/**
* 编码(将对象写入文件中)
*/
- (void)encode:(NSCoder *)encoder;
@end
/**
归档的实现
*/
#define MJCodingImplementation \
- (id)initWithCoder:(NSCoder *)decoder \
{ \
if (self = [super init]) { \
[self decode:decoder]; \
} \
return self; \
} \
\
- (void)encodeWithCoder:(NSCoder *)encoder \
{ \
[self encode:encoder]; \
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
再看实现类代码
#import "NSObject+MJCoding.h"
#import "NSObject+MJMember.h"
@implementation NSObject (MJCoding)
/**
* 编码(将对象写入文件中)
*/
- (void)encode:(NSCoder *)encoder
{
[self enumerateIvarsWithBlock:^(MJIvar *ivar, BOOL *stop) {
if (ivar.isSrcClassFromFoundation) return;
[encoder encodeObject:ivar.value forKey:ivar.name];
}];
}
/**
* 解码(从文件中解析对象)
*/
- (void)decode:(NSCoder *)decoder
{
[self enumerateIvarsWithBlock:^(MJIvar *ivar, BOOL *stop) {
if (ivar.isSrcClassFromFoundation) return;
ivar.value = [decoder decodeObjectForKey:ivar.name];
}];
}
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
它其实就帮我们实现了把所有属性都归档和反归档的操作,那么问题来了,加入我不想所有的文件都归档,我只想归档name和age属性,不想归档gay属性,那怎么办呢?其实MJExtension已经帮我们想好了,只需要调用一下:
[Person setupIgnoredCodingPropertyNames:^NSArray *{
return @[@"gay"];
}];
这个方法就可以了!!!!!!!!
最后附上MJExtension的github地址