Skip to content

Commit

Permalink
add method
Browse files Browse the repository at this point in the history
  • Loading branch information
“Bell” committed Jan 27, 2016
1 parent 2106732 commit 2600fb2
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 63 deletions.
4 changes: 2 additions & 2 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -467,7 +467,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down
2 changes: 1 addition & 1 deletion Example/ExampleUnitTests/GRBaseTestModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ - (instancetype)init
return self;
}

+ (NSArray*)gr_ignoredPropertyNames
+ (NSArray<NSString *> *)gr_ignoredPropertyNames
{
NSMutableArray *array = [[self gr_propertyNames] mutableCopy];
[array removeObject:@"base1"];
Expand Down
6 changes: 3 additions & 3 deletions Example/ExampleUnitTests/GRTestModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ + (BOOL)gr_useNullProperty
return YES;
}

+ (NSArray*)gr_ignoredPropertyNames
+ (NSArray<NSString *> *)gr_ignoredPropertyNames
{
return @[@"key0"];
}

+ (NSDictionary*)gr_replacedPropertyNames
+ (NSDictionary<NSString*, NSString*>*)gr_replacedPropertyNames
{
return @{@"key4":@"replace_key4"};
}

+ (NSDictionary *)gr_classInArray
+ (NSDictionary<NSString *, Class > *)gr_classInArray
{
return @{@"key2":[GRTestModel class]};;
}
Expand Down
9 changes: 7 additions & 2 deletions GreedJSON/GRJSONHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
+ (BOOL)isPropertyReadOnly:(Class)aClass propertyName:(NSString*)propertyName;

/**
* get the class of a property [propertyName] for the class [aClass]
* all property classes ins class
*/
+ (NSMutableDictionary <NSString *, NSMutableDictionary *> *)allPropertyClassesInClass:(Class)aClass;

/**
* get the class of the property [propertyName] for the class [aClass]
*
* @param propertyName property
* @param aClass class
*
* @return the class of a property
* @return the class of the property
*/
+ (Class)propertyClassForPropertyName:(NSString *)propertyName ofClass:(Class)aClass;

Expand Down
75 changes: 40 additions & 35 deletions GreedJSON/GRJSONHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

@implementation GRJSONHelper : NSObject

static NSMutableDictionary *propertyListByClass;
static NSMutableDictionary *propertyClassByClassAndPropertyName;
static NSMutableDictionary <NSString *, NSMutableArray *> *propertyListByClass;
static NSMutableDictionary <NSString *, NSMutableDictionary *> *propertyClassAndPropertyNameByClass;

+ (BOOL)isPropertyReadOnly:(Class)aClass propertyName:(NSString*)propertyName{
const char * type = property_getAttributes(class_getProperty(aClass, [propertyName UTF8String]));
Expand Down Expand Up @@ -87,49 +87,54 @@ + (NSArray *)propertyNames:(Class)aClass
return propertyNamesArray;
}

+ (Class)propertyClassForPropertyName:(NSString *)propertyName ofClass:(Class)aClass
+ (NSMutableDictionary <NSString *, NSMutableDictionary *> *)allPropertyClassesInClass:(Class)aClass
{
if (!propertyClassByClassAndPropertyName) {
propertyClassByClassAndPropertyName = [[NSMutableDictionary alloc] init];
if (!propertyClassAndPropertyNameByClass) {
propertyClassAndPropertyNameByClass = [[NSMutableDictionary alloc] init];
}

NSString *key = [NSString stringWithFormat:@"%@:%@", NSStringFromClass(aClass), propertyName];
id value = [propertyClassByClassAndPropertyName objectForKey:key];

if (value) {
if (value == [NSNull null]) {
return nil;
} else {
return NSClassFromString(value);
}
}

unsigned int propertyCount = 0;
objc_property_t *properties = class_copyPropertyList(aClass, &propertyCount);

const char * cPropertyName = [propertyName UTF8String];

for (unsigned int i = 0; i < propertyCount; ++i) {
objc_property_t property = properties[i];
const char * name = property_getName(property);
if (strcmp(cPropertyName, name) == 0) {
free(properties);
NSString *classKey = NSStringFromClass(aClass);
NSMutableDictionary *propertyClassByPropertyName = [propertyClassAndPropertyNameByClass objectForKey:classKey];
if (!propertyClassByPropertyName) {
propertyClassByPropertyName = [[NSMutableDictionary alloc] init];

unsigned int propertyCount = 0;
objc_property_t *properties = class_copyPropertyList(aClass, &propertyCount);

for (unsigned int i = 0; i < propertyCount; ++i) {
objc_property_t property = properties[i];
const char *name = property_getName(property);
const char *charClassName = property_getTypeName(property);
NSString *propertyName = [NSString stringWithUTF8String:name];
if (charClassName) {
NSString *className = [NSString stringWithUTF8String:charClassName];
[propertyClassByClassAndPropertyName setObject:className forKey:key];
//we found the property - we need to free
return NSClassFromString(className);
[propertyClassByPropertyName setObject:className forKey:propertyName];
} else {
Class className = [NSNumber class];
[propertyClassByClassAndPropertyName setObject:NSStringFromClass(className) forKey:key];
return className;
[propertyClassByPropertyName setObject:NSStringFromClass(className) forKey:propertyName];
}
}
[propertyClassAndPropertyNameByClass setObject:propertyClassByPropertyName forKey:classKey];
free(properties);
}
return propertyClassByPropertyName;
}

+ (Class)propertyClassForPropertyName:(NSString *)propertyName ofClass:(Class)aClass
{
if (!propertyName || !aClass) {
return nil;
}
NSMutableDictionary *propertyClassByPropertyName = [self allPropertyClassesInClass:aClass];
id value = [propertyClassByPropertyName objectForKey:propertyName];
if (value) {
if (value == [NSNull null]) {
return nil;
} else {
return NSClassFromString(value);
}
} else {
return [self propertyClassForPropertyName:propertyName ofClass:class_getSuperclass(aClass)];
}
free(properties);
//this will support traversing the inheritance chain
return [self propertyClassForPropertyName:propertyName ofClass:class_getSuperclass(aClass)];
}

+ (NSMutableArray*)allIgnoredPropertyNames:(Class)aClass
Expand Down
7 changes: 7 additions & 0 deletions GreedJSON/NSArray+GreedJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@

@interface NSArray (GreedJSON)

/**
* format NSArray to NSString
*/
- (NSString*)gr_JSONString;

/**
* format NSArray to NSData
*/
- (NSData*)gr_JSONData;

@end
4 changes: 1 addition & 3 deletions GreedJSON/NSData+GreedJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
@interface NSData (GreedJSON)

/**
* format to NSDictionary or NSArray
*
* @return NSDictionary or NSArray
* format NSData to NSDictionary or NSArray
*/
- (__kindof NSObject*)gr_object;

Expand Down
7 changes: 7 additions & 0 deletions GreedJSON/NSDictionary+GreedJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@

@interface NSDictionary (GreedJSON)

/**
* format NSDictionary to NSString
*/
- (NSString*)gr_JSONString;

/**
* format NSDictionary to NSData
*/
- (NSData*)gr_JSONData;

@end
26 changes: 15 additions & 11 deletions GreedJSON/NSObject+GreedJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
#pragma mark - property getter

/**
* the property names only in this class
* all property names only in this class
*/
- (NSArray*)gr_propertyNames;

/**
* the property names in this class and all super classes
* all property names in this class and all super classes
*/
- (NSMutableArray*)gr_allPropertyNames;

Expand All @@ -36,36 +36,40 @@
*/
+ (BOOL)gr_useNullProperty;

+ (NSArray*)gr_ignoredPropertyNames;
/**
* get all ignored property names
*
*/
+ (NSArray<NSString *> *)gr_ignoredPropertyNames;

/**
* @{propertyName:dictionaryKey}
*
*/
+ (NSDictionary*)gr_replacedPropertyNames;
+ (NSDictionary<NSString *, NSString *> *)gr_replacedPropertyNames;

+ (NSDictionary *)gr_classInArray;
/**
* class in array
*/
+ (NSDictionary<NSString *, Class > *)gr_classInArray;

#pragma mark - Foundation

- (BOOL)gr_isFromFoundation;

#pragma mark - parse
#pragma mark - format

/**
* update Model with NSDictionary
*/
- (instancetype)gr_setDictionary:(NSDictionary*)dictionary;

/**
* NSDictionary to Model
* init Model with NSDictionary
*/
+ (id)gr_objectFromDictionary:(NSDictionary*)dictionary;

/**
* get NSMutableDictionary for the model based from NSObject, if gr_isFromFoundation will return self
*
* @return if gr_isFromFoundation return self,else return NSMutableDictionary
* get NSMutableDictionary from model, if gr_isFromFoundation return self
*/
- (__kindof NSObject *)gr_dictionary;

Expand Down
8 changes: 3 additions & 5 deletions GreedJSON/NSObject+GreedJSON.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ + (BOOL)gr_useNullProperty
return NO;
}

+ (NSArray*)gr_ignoredPropertyNames
+ (NSArray<NSString *> *)gr_ignoredPropertyNames
{
NSArray *array = nil;
Class superClass = class_getSuperclass([self class]);
Expand All @@ -56,7 +56,7 @@ + (NSArray*)gr_ignoredPropertyNames
return array;
}

+ (NSDictionary*)gr_replacedPropertyNames
+ (NSDictionary<NSString *, NSString *> *)gr_replacedPropertyNames
{
NSDictionary *dictionary = nil;
Class superClass = class_getSuperclass([self class]);
Expand All @@ -66,7 +66,7 @@ + (NSDictionary*)gr_replacedPropertyNames
return dictionary;
}

+ (NSDictionary *)gr_classInArray
+ (NSDictionary<NSString *, Class > *)gr_classInArray
{
NSDictionary *dictionary = nil;
Class superClass = class_getSuperclass([self class]);
Expand Down Expand Up @@ -237,11 +237,9 @@ - (__kindof NSObject *)gr_dictionary
}
}
}];

return dic;
}

#pragma mark - private


@end
2 changes: 1 addition & 1 deletion GreedJSON/NSString+GreedJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@interface NSString (GreedJSON)

/**
* format to NSDictionary or NSArray
* format NSString to NSDictionary or NSArray
*
* @return NSDictionary or NSArray
*/
Expand Down

0 comments on commit 2600fb2

Please sign in to comment.