Skip to content

Commit

Permalink
Faster and faster
Browse files Browse the repository at this point in the history
Faster and faster
  • Loading branch information
CoderMJLee committed Aug 14, 2015
1 parent fd99848 commit 9706d28
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion MJExtension.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "MJExtension"
s.version = "2.5.6"
s.version = "2.5.7"
s.ios.deployment_target = '6.0'
s.osx.deployment_target = '10.8'
s.summary = "The fastest and most convenient conversion between JSON and model"
Expand Down
2 changes: 1 addition & 1 deletion MJExtension/NSObject+MJClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ typedef NSArray * (^MJIgnoredCodingPropertyNames)();
+ (NSMutableArray *)totalIgnoredCodingPropertyNames;

#pragma mark - 内部使用
+ (void)setupBlockReturnValue:(id (^)())block key:(const char *)key;
+ (void)setupBlockReturnValue:(id (^)())block key:(const char *)key dict:(NSMutableDictionary *)dict;
@end
41 changes: 30 additions & 11 deletions MJExtension/NSObject+MJClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@

@implementation NSObject (MJClass)

static NSMutableDictionary *allowedPropertyNames_;
static NSMutableDictionary *ignoredPropertyNames_;
static NSMutableDictionary *allowedCodingPropertyNames_;
static NSMutableDictionary *ignoredCodingPropertyNames_;
+ (void)load
{
allowedPropertyNames_ = [NSMutableDictionary dictionary];
ignoredPropertyNames_ = [NSMutableDictionary dictionary];
allowedCodingPropertyNames_ = [NSMutableDictionary dictionary];
ignoredCodingPropertyNames_ = [NSMutableDictionary dictionary];
}

+ (void)enumerateClasses:(MJClassesEnumeration)enumeration
{
// 1.没有block就直接返回
Expand Down Expand Up @@ -66,59 +78,66 @@ + (void)enumerateAllClasses:(MJClassesEnumeration)enumeration
#pragma mark - 属性黑名单配置
+ (void)setupIgnoredPropertyNames:(MJIgnoredPropertyNames)ignoredPropertyNames
{
[self setupBlockReturnValue:ignoredPropertyNames key:&MJIgnoredPropertyNamesKey];
[self setupBlockReturnValue:ignoredPropertyNames key:&MJIgnoredPropertyNamesKey dict:ignoredPropertyNames_];
}

+ (NSMutableArray *)totalIgnoredPropertyNames
{
return [self totalObjectsWithSelector:@selector(ignoredPropertyNames) key:&MJIgnoredPropertyNamesKey];
return [self totalObjectsWithSelector:@selector(ignoredPropertyNames) key:&MJIgnoredPropertyNamesKey dict:ignoredPropertyNames_];
}

#pragma mark - 归档属性黑名单配置
+ (void)setupIgnoredCodingPropertyNames:(MJIgnoredCodingPropertyNames)ignoredCodingPropertyNames
{
[self setupBlockReturnValue:ignoredCodingPropertyNames key:&MJIgnoredCodingPropertyNamesKey];
[self setupBlockReturnValue:ignoredCodingPropertyNames key:&MJIgnoredCodingPropertyNamesKey dict:ignoredCodingPropertyNames_];
}

+ (NSMutableArray *)totalIgnoredCodingPropertyNames
{
return [self totalObjectsWithSelector:@selector(ignoredCodingPropertyNames) key:&MJIgnoredCodingPropertyNamesKey];
return [self totalObjectsWithSelector:@selector(ignoredCodingPropertyNames) key:&MJIgnoredCodingPropertyNamesKey dict:ignoredCodingPropertyNames_];
}

#pragma mark - 属性白名单配置
+ (void)setupAllowedPropertyNames:(MJAllowedPropertyNames)allowedPropertyNames;
{
[self setupBlockReturnValue:allowedPropertyNames key:&MJAllowedPropertyNamesKey];
[self setupBlockReturnValue:allowedPropertyNames key:&MJAllowedPropertyNamesKey dict:allowedPropertyNames_];
}

+ (NSMutableArray *)totalAllowedPropertyNames
{
return [self totalObjectsWithSelector:@selector(allowedPropertyNames) key:&MJAllowedPropertyNamesKey];
return [self totalObjectsWithSelector:@selector(allowedPropertyNames) key:&MJAllowedPropertyNamesKey dict:allowedPropertyNames_];
}

#pragma mark - 归档属性白名单配置
+ (void)setupAllowedCodingPropertyNames:(MJAllowedCodingPropertyNames)allowedCodingPropertyNames
{
[self setupBlockReturnValue:allowedCodingPropertyNames key:&MJAllowedCodingPropertyNamesKey];
[self setupBlockReturnValue:allowedCodingPropertyNames key:&MJAllowedCodingPropertyNamesKey dict:allowedCodingPropertyNames_];
}

+ (NSMutableArray *)totalAllowedCodingPropertyNames
{
return [self totalObjectsWithSelector:@selector(allowedCodingPropertyNames) key:&MJAllowedCodingPropertyNamesKey];
return [self totalObjectsWithSelector:@selector(allowedCodingPropertyNames) key:&MJAllowedCodingPropertyNamesKey dict:allowedCodingPropertyNames_];
}
#pragma mark - block和方法处理:存储block的返回值
+ (void)setupBlockReturnValue:(id (^)())block key:(const char *)key
+ (void)setupBlockReturnValue:(id (^)())block key:(const char *)key dict:(NSMutableDictionary *)dict
{
if (block) {
objc_setAssociatedObject(self, key, block(), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} else {
objc_setAssociatedObject(self, key, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

// 清空数据
[dict removeAllObjects];
}

+ (NSMutableArray *)totalObjectsWithSelector:(SEL)selector key:(const char *)key
+ (NSMutableArray *)totalObjectsWithSelector:(SEL)selector key:(const char *)key dict:(NSMutableDictionary *)dict
{
NSMutableArray *array = [NSMutableArray array];
NSMutableArray *array = dict[NSStringFromClass(self)];
if (array) return array;

// 创建、存储
dict[NSStringFromClass(self)] = array = [NSMutableArray array];

if ([self respondsToSelector:selector]) {
#pragma clang diagnostic push
Expand Down
3 changes: 3 additions & 0 deletions MJExtension/NSObject+MJCoding.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ - (void)decode:(NSCoder *)decoder
if ([ignoredCodingPropertyNames containsObject:property.name]) return;

id value = [decoder decodeObjectForKey:property.name];
if (value == nil) { // 兼容以前的MJExtension版本
value = [decoder decodeObjectForKey:[@"_" stringByAppendingString:property.name]];
}
if (value == nil) return;
[property setValue:value forObject:self];
}];
Expand Down
4 changes: 2 additions & 2 deletions MJExtension/NSObject+MJProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ + (id)getNewValueFromObject:(__weak id)object oldValue:(__weak id)oldValue prope
#pragma mark - array model class配置
+ (void)setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray
{
[self setupBlockReturnValue:objectClassInArray key:&MJObjectClassInArrayKey];
[self setupBlockReturnValue:objectClassInArray key:&MJObjectClassInArrayKey dict:nil];
[cachedProperties_ removeAllObjects];
}

#pragma mark - key配置
+ (void)setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)replacedKeyFromPropertyName
{
[self setupBlockReturnValue:replacedKeyFromPropertyName key:&MJReplacedKeyFromPropertyNameKey];
[self setupBlockReturnValue:replacedKeyFromPropertyName key:&MJReplacedKeyFromPropertyNameKey dict:nil];
[cachedProperties_ removeAllObjects];
}

Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion MJExtensionExample/MJExtensionExample/Classes/Bag.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@implementation Bag
// NSCoding实现
MJCodingImplementation
MJExtensionCodingImplementation

//+ (NSArray *)ignoredCodingPropertyNames
//{
Expand Down
2 changes: 1 addition & 1 deletion MJExtensionExample/MJExtensionExample/Classes/Box.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
#import "MJExtension.h"

@implementation Box
MJCodingImplementation
MJExtensionCodingImplementation
@end

0 comments on commit 9706d28

Please sign in to comment.