Skip to content

Commit

Permalink
增强key替换的功能
Browse files Browse the repository at this point in the history
1.优先调用mj_replacedKeyFromPropertyName121
2.其次调用mj_replacedKeyFromPropertyName
  • Loading branch information
520dev committed Jun 30, 2016
1 parent 4bce9b7 commit 498303d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion MJExtension.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "MJExtension"
s.version = "3.0.10"
s.version = "3.0.11"
s.ios.deployment_target = '6.0'
s.osx.deployment_target = '10.8'
s.summary = "A fast and convenient conversion between JSON and model"
Expand Down
2 changes: 1 addition & 1 deletion MJExtension/NSObject+MJKeyValue.h
Expand Up @@ -38,7 +38,7 @@
*
* @return 从字典中取值用的key
*/
+ (NSString *)mj_replacedKeyFromPropertyName121:(NSString *)propertyName;
+ (id)mj_replacedKeyFromPropertyName121:(NSString *)propertyName;

/**
* 数组中需要转换的模型类
Expand Down
2 changes: 1 addition & 1 deletion MJExtension/NSObject+MJProperty.h
Expand Up @@ -21,7 +21,7 @@ typedef void (^MJPropertiesEnumeration)(MJProperty *property, BOOL *stop);

/** 将属性名换为其他key去字典中取值 */
typedef NSDictionary * (^MJReplacedKeyFromPropertyName)();
typedef NSString * (^MJReplacedKeyFromPropertyName121)(NSString *propertyName);
typedef id (^MJReplacedKeyFromPropertyName121)(NSString *propertyName);
/** 数组中需要转换的模型类 */
typedef NSDictionary * (^MJObjectClassInArray)();
/** 用于过滤字典中的值 */
Expand Down
12 changes: 6 additions & 6 deletions MJExtension/NSObject+MJProperty.m
Expand Up @@ -53,11 +53,11 @@ + (NSMutableDictionary *)dictForKey:(const void *)key
}

#pragma mark - --私有方法--
+ (NSString *)propertyKey:(NSString *)propertyName
+ (id)propertyKey:(NSString *)propertyName
{
MJExtensionAssertParamNotNil2(propertyName, nil);

__block NSString *key = nil;
__block id key = nil;
// 查看有没有需要替换的key
if ([self respondsToSelector:@selector(mj_replacedKeyFromPropertyName121:)]) {
key = [self mj_replacedKeyFromPropertyName121:propertyName];
Expand All @@ -79,21 +79,21 @@ + (NSString *)propertyKey:(NSString *)propertyName
}

// 查看有没有需要替换的key
if (!key && [self respondsToSelector:@selector(mj_replacedKeyFromPropertyName)]) {
if ((!key || [key isEqual:propertyName]) && [self respondsToSelector:@selector(mj_replacedKeyFromPropertyName)]) {
key = [self mj_replacedKeyFromPropertyName][propertyName];
}
// 兼容旧版本
if (!key && [self respondsToSelector:@selector(replacedKeyFromPropertyName)]) {
if ((!key || [key isEqual:propertyName]) && [self respondsToSelector:@selector(replacedKeyFromPropertyName)]) {
key = [self performSelector:@selector(replacedKeyFromPropertyName)][propertyName];
}

if (!key) {
if (!key || [key isEqual:propertyName]) {
[self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
NSDictionary *dict = objc_getAssociatedObject(c, &MJReplacedKeyFromPropertyNameKey);
if (dict) {
key = dict[propertyName];
}
if (key) *stop = YES;
if (key && ![key isEqual:propertyName]) *stop = YES;
}];
}

Expand Down

0 comments on commit 498303d

Please sign in to comment.