Skip to content

Commit

Permalink
Merge branch 'pr/131'
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderMJLee committed Jul 16, 2015
2 parents f6a861a + 2a5fecb commit 662cae5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
11 changes: 9 additions & 2 deletions MJExtension/MJFoundation.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ @implementation MJFoundation
+ (NSSet *)foundatonClasses
{
if (_foundationClasses == nil) {
// 集合中没有NSObject,因为几乎所有的类都是继承自NSObject,具体是不是NSObject需要特殊判断
_foundationClasses = [NSSet setWithObjects:
[NSURL class],
[NSDate class],
Expand All @@ -33,11 +34,17 @@ + (NSSet *)foundatonClasses
+ (BOOL)isClassFromFoundation:(Class)c
{
__block BOOL result = NO;
[[self foundatonClasses] enumerateObjectsUsingBlock:^(Class obj, BOOL *stop) {
if (c == [NSObject class] || c == obj || [c isSubclassOfClass:obj]) {
[[self foundatonClasses] enumerateObjectsUsingBlock:^(Class foundationClass, BOOL *stop) {
if (c == foundationClass || [c isSubclassOfClass:foundationClass]) {
result = YES;
*stop = YES;
}
}];

if (c == [NSObject class]) {
result = YES;
}

return result;
}
@end
15 changes: 12 additions & 3 deletions MJExtension/MJProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,29 @@ typedef enum {
MJPropertyKeyTypeDictionary = 0, // 字典的key
MJPropertyKeyTypeArray // 数组的key
} MJPropertyKeyType;


/**
* 属性的key
*/
@interface MJPropertyKey : NSObject
@property (copy, nonatomic) NSString *name;

@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) MJPropertyKeyType type;

/**
* 根据当前的key从object(字典或者数组)中取值
* 根据当前的key,也就是name,从object(字典或者数组)中取值
*/
- (id)valueForObject:(id)object;
- (id)valueInObject:(id)object;

@end


/**
* 包装一个成员
*/
@interface MJProperty : NSObject

/** 成员属性 */
@property (nonatomic, assign) objc_property_t property;
/** 成员属性名 */
Expand All @@ -40,6 +47,7 @@ typedef enum {
/** 成员来源于哪个类(可能是父类) */
@property (nonatomic, assign) Class srcClass;


/**** 同一个成员变量 - 父类和子类的行为可能不一致(key、keys、objectClassInArray) ****/
/** 对应着字典中的key */
- (void)setKey:(NSString *)key forClass:(Class)c;
Expand All @@ -64,4 +72,5 @@ typedef enum {
* 初始化
*/
+ (instancetype)cachedPropertyWithProperty:(objc_property_t)property;

@end
4 changes: 3 additions & 1 deletion MJExtension/MJProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@implementation MJPropertyKey

- (id)valueForObject:(id)object
- (id)valueInObject:(id)object
{
if ([object isKindOfClass:[NSDictionary class]] && self.type == MJPropertyKeyTypeDictionary) {
return object[self.name];
Expand All @@ -25,8 +25,10 @@ - (id)valueForObject:(id)object
@end

@interface MJProperty()

@property (strong, nonatomic) NSMutableDictionary *propertyKeysDict;
@property (strong, nonatomic) NSMutableDictionary *objectClassInArrayDict;

@end

@implementation MJProperty
Expand Down
4 changes: 2 additions & 2 deletions MJExtension/NSObject+MJKeyValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)con
id value = keyValues ;
NSArray *propertyKeys = [property propertyKeysFromClass:[self class]];
for (MJPropertyKey *propertyKey in propertyKeys) {
value = [propertyKey valueForObject:value];
value = [propertyKey valueInObject:value];
}

// 值的过滤
Expand Down Expand Up @@ -350,7 +350,7 @@ - (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys ignoredKeys:(NSArray

if (nextPropertyKey) { // 不是最后一个key
// 当前propertyKey对应的字典或者数组
id tempInnerContainer = [propertyKey valueForObject:innerContainer];
id tempInnerContainer = [propertyKey valueInObject:innerContainer];
if (tempInnerContainer == nil || [tempInnerContainer isKindOfClass:[NSNull class]]) {
if (nextPropertyKey.type == MJPropertyKeyTypeDictionary) {
tempInnerContainer = [NSMutableDictionary dictionary];
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ NSDictionary *dict = @{
@"info" : @[
@"test-data",
@{
@"nameChangedTime" : @"2013-08"
}
}]
@"nameChangedTime" : @"2013-08"
}
]
},
@"other" : @{
@"bag" : @{
Expand Down

0 comments on commit 662cae5

Please sign in to comment.