Skip to content

Commit

Permalink
referenceReplacedKeyWhenCreatingKeyValues: can be extends
Browse files Browse the repository at this point in the history
referenceReplacedKeyWhenCreatingKeyValues: can be extends
  • Loading branch information
CoderMJLee committed Jul 24, 2015
1 parent f8262dd commit 2b5aa56
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 60 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.1"
s.version = "2.5.3"
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
31 changes: 12 additions & 19 deletions MJExtension/MJConst.h → MJExtension/MJExtensionConst.h
Original file line number Diff line number Diff line change
@@ -1,57 +1,49 @@

#ifndef __MJConst__H__
#define __MJConst__H__
#ifndef __MJExtensionConst__H__
#define __MJExtensionConst__H__

#import <Foundation/Foundation.h>

// 过期
#define MJDeprecated(instead) NS_DEPRECATED(2_0, 2_0, 2_0, 2_0, instead)

#ifdef DEBUG // 调试状态
// 打开LOG功能
#define MJLog(...) NSLog(__VA_ARGS__)
#else // 发布状态
// 关闭LOG功能
#define MJLog(...)
#endif
#define MJExtensionDeprecated(instead) NS_DEPRECATED(2_0, 2_0, 2_0, 2_0, instead)

// 构建错误
#define MJBuildError(error, msg) \
#define MJExtensionBuildError(error, msg) \
if (error) *error = [NSError errorWithDomain:msg code:250 userInfo:nil];

/**
* 断言
* @param condition 条件
* @param returnValue 返回值
*/
#define MJAssertError(condition, returnValue, error, msg) \
#define MJExtensionAssertError(condition, returnValue, error, msg) \
if ((condition) == NO) { \
MJBuildError(error, msg); \
MJExtensionBuildError(error, msg); \
return returnValue;\
}

#define MJAssert2(condition, returnValue) \
#define MJExtensionAssert2(condition, returnValue) \
if ((condition) == NO) return returnValue;

/**
* 断言
* @param condition 条件
*/
#define MJAssert(condition) MJAssert2(condition, )
#define MJExtensionAssert(condition) MJExtensionAssert2(condition, )

/**
* 断言
* @param param 参数
* @param returnValue 返回值
*/
#define MJAssertParamNotNil2(param, returnValue) \
MJAssert2((param) != nil, returnValue)
#define MJExtensionAssertParamNotNil2(param, returnValue) \
MJExtensionAssert2((param) != nil, returnValue)

/**
* 断言
* @param param 参数
*/
#define MJAssertParamNotNil(param) MJAssertParamNotNil2(param, )
#define MJExtensionAssertParamNotNil(param) MJExtensionAssertParamNotNil2(param, )

/**
* 打印所有的属性
Expand All @@ -61,6 +53,7 @@ MJAssert2((param) != nil, returnValue)
{ \
return [self keyValues].description; \
}
#define MJExtensionLogAllIvars MJLogAllIvars

/**
* 类型(属性类型)
Expand Down
4 changes: 2 additions & 2 deletions MJExtension/MJConst.m → MJExtension/MJExtensionConst.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __MJConst__M__
#define __MJConst__M__
#ifndef __MJExtensionConst__M__
#define __MJExtensionConst__M__

#import <Foundation/Foundation.h>

Expand Down
2 changes: 1 addition & 1 deletion MJExtension/MJFoundation.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "MJFoundation.h"
#import "MJConst.h"
#import "MJExtensionConst.h"
#import <CoreData/CoreData.h>

static NSSet *_foundationClasses;
Expand Down
4 changes: 2 additions & 2 deletions MJExtension/MJProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "MJProperty.h"
#import "MJFoundation.h"
#import "MJConst.h"
#import "MJExtensionConst.h"

@implementation MJPropertyKey

Expand Down Expand Up @@ -64,7 +64,7 @@ - (void)setProperty:(objc_property_t)property
{
_property = property;

MJAssertParamNotNil(property);
MJExtensionAssertParamNotNil(property);

// 1.属性名
_name = @(property_getName(property));
Expand Down
6 changes: 3 additions & 3 deletions MJExtension/MJType.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import "MJType.h"
#import "MJExtension.h"
#import "MJFoundation.h"
#import "MJConst.h"
#import "MJExtensionConst.h"

@implementation MJType

Expand All @@ -21,7 +21,7 @@ + (void)load

+ (instancetype)cachedTypeWithCode:(NSString *)code
{
MJAssertParamNotNil2(code, nil);
MJExtensionAssertParamNotNil2(code, nil);

MJType *type = _cachedTypes[code];
if (type == nil) {
Expand All @@ -36,7 +36,7 @@ - (void)setCode:(NSString *)code
{
_code = code;

MJAssertParamNotNil(code);
MJExtensionAssertParamNotNil(code);

if ([code isEqualToString:MJTypeId]) {
_idType = YES;
Expand Down
4 changes: 3 additions & 1 deletion MJExtension/NSObject+MJCoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ return self; \
- (void)encodeWithCoder:(NSCoder *)encoder \
{ \
[self encode:encoder]; \
}
}

#define MJExtensionCodingImplementation MJCodingImplementation
5 changes: 2 additions & 3 deletions MJExtension/NSObject+MJKeyValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import <Foundation/Foundation.h>
#import "MJConst.h"
#import "MJExtensionConst.h"
#import <CoreData/CoreData.h>
#import "MJProperty.h"

Expand Down Expand Up @@ -84,10 +84,9 @@
- (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context error:(NSError **)error;

/**
* 模型转字典时,字典的key是否参考replacedKeyFromPropertyName等方法
* 模型转字典时,字典的key是否参考replacedKeyFromPropertyName等方法(父类设置了,子类也会继承下来)
*/
+ (void)referenceReplacedKeyWhenCreatingKeyValues:(BOOL)reference;
+ (BOOL)isReferenceReplacedKeyWhenCreatingKeyValues;

/**
* 将模型转成字典
Expand Down
30 changes: 19 additions & 11 deletions MJExtension/NSObject+MJKeyValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import "NSObject+MJProperty.h"
#import "MJProperty.h"
#import "MJType.h"
#import "MJConst.h"
#import "MJExtensionConst.h"
#import "MJFoundation.h"

@implementation NSObject (MJKeyValue)
Expand All @@ -24,7 +24,15 @@ + (void)referenceReplacedKeyWhenCreatingKeyValues:(BOOL)reference

+ (BOOL)isReferenceReplacedKeyWhenCreatingKeyValues
{
return [objc_getAssociatedObject(self, &MJReferenceReplacedKeyWhenCreatingKeyValuesKey) boolValue];
__block id value = objc_getAssociatedObject(self, &MJReferenceReplacedKeyWhenCreatingKeyValuesKey);
if (!value) {
[self enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
value = objc_getAssociatedObject(c, &MJReferenceReplacedKeyWhenCreatingKeyValuesKey);

if (value) *stop = YES;
}];
}
return [value boolValue];
}

#pragma mark - --常用的对象--
Expand Down Expand Up @@ -67,7 +75,7 @@ + (instancetype)objectWithFilename:(NSString *)filename

+ (instancetype)objectWithFilename:(NSString *)filename error:(NSError *__autoreleasing *)error
{
MJAssertError(filename != nil, nil, error, @"filename参数为nil");
MJExtensionAssertError(filename != nil, nil, error, @"filename参数为nil");

return [self objectWithFile:[[NSBundle mainBundle] pathForResource:filename ofType:nil] error:error];
}
Expand All @@ -79,7 +87,7 @@ + (instancetype)objectWithFile:(NSString *)file

+ (instancetype)objectWithFile:(NSString *)file error:(NSError *__autoreleasing *)error
{
MJAssertError(file != nil, nil, error, @"file参数为nil");
MJExtensionAssertError(file != nil, nil, error, @"file参数为nil");

return [self objectWithKeyValues:[NSDictionary dictionaryWithContentsOfFile:file] error:error];
}
Expand Down Expand Up @@ -111,7 +119,7 @@ - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)con
keyValues = [NSJSONSerialization JSONObjectWithData:keyValues options:kNilOptions error:nil];
}

MJAssertError([keyValues isKindOfClass:[NSDictionary class]], self, error, @"keyValues参数不是一个字典");
MJExtensionAssertError([keyValues isKindOfClass:[NSDictionary class]], self, error, @"keyValues参数不是一个字典");

@try {
Class aClass = [self class];
Expand Down Expand Up @@ -193,7 +201,7 @@ - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)con
[self keyValuesDidFinishConvertingToObject];
}
} @catch (NSException *exception) {
MJBuildError(error, exception.reason);
MJExtensionBuildError(error, exception.reason);
}
return self;
}
Expand Down Expand Up @@ -227,7 +235,7 @@ + (NSMutableArray *)objectArrayWithKeyValuesArray:(id)keyValuesArray context:(NS
}

// 1.判断真实性
MJAssertError([keyValuesArray isKindOfClass:[NSArray class]], nil, error, @"keyValuesArray参数不是一个数组");
MJExtensionAssertError([keyValuesArray isKindOfClass:[NSArray class]], nil, error, @"keyValuesArray参数不是一个数组");

// 2.创建数组
NSMutableArray *modelArray = [NSMutableArray array];
Expand All @@ -252,7 +260,7 @@ + (NSMutableArray *)objectArrayWithFilename:(NSString *)filename

+ (NSMutableArray *)objectArrayWithFilename:(NSString *)filename error:(NSError *__autoreleasing *)error
{
MJAssertError(filename != nil, nil, error, @"filename参数为nil");
MJExtensionAssertError(filename != nil, nil, error, @"filename参数为nil");

return [self objectArrayWithFile:[[NSBundle mainBundle] pathForResource:filename ofType:nil] error:error];
}
Expand All @@ -264,7 +272,7 @@ + (NSMutableArray *)objectArrayWithFile:(NSString *)file

+ (NSMutableArray *)objectArrayWithFile:(NSString *)file error:(NSError *__autoreleasing *)error
{
MJAssertError(file != nil, nil, error, @"file参数为nil");
MJExtensionAssertError(file != nil, nil, error, @"file参数为nil");

return [self objectArrayWithKeyValuesArray:[NSArray arrayWithContentsOfFile:file] error:error];
}
Expand Down Expand Up @@ -395,7 +403,7 @@ - (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys ignoredKeys:(NSArray
[self objectDidFinishConvertingToKeyValues];
}
} @catch (NSException *exception) {
MJBuildError(error, exception.reason);
MJExtensionBuildError(error, exception.reason);
}

return keyValues;
Expand Down Expand Up @@ -434,7 +442,7 @@ + (NSMutableArray *)keyValuesArrayWithObjectArray:(NSArray *)objectArray ignored
+ (NSMutableArray *)keyValuesArrayWithObjectArray:(NSArray *)objectArray keys:(NSArray *)keys ignoredKeys:(NSArray *)ignoredKeys error:(NSError *__autoreleasing *)error
{
// 0.判断真实性
MJAssertError([objectArray isKindOfClass:[NSArray class]], nil, error, @"objectArray参数不是一个数组");
MJExtensionAssertError([objectArray isKindOfClass:[NSArray class]], nil, error, @"objectArray参数不是一个数组");

// 1.创建数组
NSMutableArray *keyValuesArray = [NSMutableArray array];
Expand Down
2 changes: 1 addition & 1 deletion MJExtension/NSObject+MJProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ @implementation NSObject (Property)
#pragma mark - --私有方法--
+ (NSString *)propertyKey:(NSString *)propertyName
{
MJAssertParamNotNil2(propertyName, nil);
MJExtensionAssertParamNotNil2(propertyName, nil);

__block NSString *key = nil;
// 查看有没有需要替换的key
Expand Down
18 changes: 12 additions & 6 deletions MJExtensionExample/MJExtensionExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
2D306F5D1B58A6650069910E /* MJExtensionConst.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D306F5C1B58A6650069910E /* MJExtensionConst.m */; };
2D306F601B5A24430069910E /* BaseObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D306F5F1B5A24430069910E /* BaseObject.m */; };
2D35374C1B24868D00D5B7A8 /* Dog.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D35374B1B24868D00D5B7A8 /* Dog.m */; };
2D3537551B248D9B00D5B7A8 /* Book.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D3537541B248D9B00D5B7A8 /* Book.m */; };
2D3576231A7913F30056D6BF /* Bag.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D3576221A7913F30056D6BF /* Bag.m */; };
2D49F3211B2855EC008D5A72 /* Box.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D49F3201B2855EC008D5A72 /* Box.m */; };
2D4A0BF81B25615700D323A7 /* MJConst.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4A0BE81B25615600D323A7 /* MJConst.m */; };
2D4A0BF91B25615700D323A7 /* MJFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4A0BEB1B25615600D323A7 /* MJFoundation.m */; };
2D4A0BFA1B25615700D323A7 /* MJProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4A0BED1B25615600D323A7 /* MJProperty.m */; };
2D4A0BFB1B25615700D323A7 /* MJType.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4A0BEF1B25615700D323A7 /* MJType.m */; };
Expand Down Expand Up @@ -46,6 +47,10 @@

/* Begin PBXFileReference section */
2D2ABF0B1A7127DC0016C673 /* main.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = main.h; sourceTree = "<group>"; };
2D306F5B1B58A6650069910E /* MJExtensionConst.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJExtensionConst.h; sourceTree = "<group>"; };
2D306F5C1B58A6650069910E /* MJExtensionConst.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJExtensionConst.m; sourceTree = "<group>"; };
2D306F5E1B5A24430069910E /* BaseObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BaseObject.h; sourceTree = "<group>"; };
2D306F5F1B5A24430069910E /* BaseObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BaseObject.m; sourceTree = "<group>"; };
2D35374A1B24868D00D5B7A8 /* Dog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Dog.h; sourceTree = "<group>"; };
2D35374B1B24868D00D5B7A8 /* Dog.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Dog.m; sourceTree = "<group>"; };
2D3537531B248D9B00D5B7A8 /* Book.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Book.h; sourceTree = "<group>"; };
Expand All @@ -54,8 +59,6 @@
2D3576221A7913F30056D6BF /* Bag.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Bag.m; sourceTree = "<group>"; };
2D49F31F1B2855EC008D5A72 /* Box.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Box.h; sourceTree = "<group>"; };
2D49F3201B2855EC008D5A72 /* Box.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Box.m; sourceTree = "<group>"; };
2D4A0BE71B25615600D323A7 /* MJConst.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJConst.h; sourceTree = "<group>"; };
2D4A0BE81B25615600D323A7 /* MJConst.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJConst.m; sourceTree = "<group>"; };
2D4A0BE91B25615600D323A7 /* MJExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJExtension.h; sourceTree = "<group>"; };
2D4A0BEA1B25615600D323A7 /* MJFoundation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJFoundation.h; sourceTree = "<group>"; };
2D4A0BEB1B25615600D323A7 /* MJFoundation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJFoundation.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -107,8 +110,8 @@
2D4A0BE61B25615600D323A7 /* MJExtension */ = {
isa = PBXGroup;
children = (
2D4A0BE71B25615600D323A7 /* MJConst.h */,
2D4A0BE81B25615600D323A7 /* MJConst.m */,
2D306F5B1B58A6650069910E /* MJExtensionConst.h */,
2D306F5C1B58A6650069910E /* MJExtensionConst.m */,
2D4A0BE91B25615600D323A7 /* MJExtension.h */,
2D4A0BEA1B25615600D323A7 /* MJFoundation.h */,
2D4A0BEB1B25615600D323A7 /* MJFoundation.m */,
Expand Down Expand Up @@ -152,6 +155,8 @@
2D3537541B248D9B00D5B7A8 /* Book.m */,
2D49F31F1B2855EC008D5A72 /* Box.h */,
2D49F3201B2855EC008D5A72 /* Box.m */,
2D306F5E1B5A24430069910E /* BaseObject.h */,
2D306F5F1B5A24430069910E /* BaseObject.m */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -255,6 +260,7 @@
buildActionMask = 2147483647;
files = (
2D4A0BFD1B25615700D323A7 /* NSObject+MJKeyValue.m in Sources */,
2D306F601B5A24430069910E /* BaseObject.m in Sources */,
2D6CAFF91A5AA0BB00E2BA5E /* User.m in Sources */,
2D6CAFF61A5AA0BB00E2BA5E /* Ad.m in Sources */,
2D6CAFFC1A5AA34800E2BA5E /* Student.m in Sources */,
Expand All @@ -267,9 +273,9 @@
2D9CAF2E192FA6020011F500 /* main.m in Sources */,
2D4B069E1AE7CFF300C1843E /* MJExtensionConfig.m in Sources */,
2D49F3211B2855EC008D5A72 /* Box.m in Sources */,
2D4A0BF81B25615700D323A7 /* MJConst.m in Sources */,
2D3537551B248D9B00D5B7A8 /* Book.m in Sources */,
2D4A0BFE1B25615700D323A7 /* NSObject+MJProperty.m in Sources */,
2D306F5D1B58A6650069910E /* MJExtensionConst.m in Sources */,
2D35374C1B24868D00D5B7A8 /* Dog.m in Sources */,
2D3576231A7913F30056D6BF /* Bag.m in Sources */,
2D4A0BFC1B25615700D323A7 /* NSObject+MJCoding.m in Sources */,
Expand Down
Binary file not shown.
13 changes: 13 additions & 0 deletions MJExtensionExample/MJExtensionExample/Classes/BaseObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// BaseObject.h
// MJExtensionExample
//
// Created by MJ Lee on 15/7/18.
// Copyright (c) 2015年 小码哥. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BaseObject : NSObject
@property (copy, nonatomic) NSString *name;
@end
13 changes: 13 additions & 0 deletions MJExtensionExample/MJExtensionExample/Classes/BaseObject.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// BaseObject.m
// MJExtensionExample
//
// Created by MJ Lee on 15/7/18.
// Copyright (c) 2015年 小码哥. All rights reserved.
//

#import "BaseObject.h"

@implementation BaseObject

@end
Loading

0 comments on commit 2b5aa56

Please sign in to comment.