no message

This commit is contained in:
Giuseppe Nucifora 2016-04-18 12:27:24 +02:00
parent 2e2ed83e95
commit 79b96fc967
2 changed files with 73 additions and 63 deletions

View File

@ -8,7 +8,7 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = "PNObject" s.name = "PNObject"
s.version = "0.6.0" s.version = "0.6.1"
s.summary = "PNObject is a simple replica of the more complex ParseObject" s.summary = "PNObject is a simple replica of the more complex ParseObject"
# This description is used to generate tags and improve search results. # This description is used to generate tags and improve search results.

View File

@ -29,36 +29,36 @@
} }
- (void) populateObjectFromJSON:(id _Nullable)JSON fromLocal:(BOOL) fromLocal { - (void) populateObjectFromJSON:(id _Nullable)JSON fromLocal:(BOOL) fromLocal {
NSDictionary *properties = [PNObject propertiesForClass:self.class]; NSDictionary *properties = [PNObject propertiesForClass:self.class];
for(NSString *propertyName in properties) { for(NSString *propertyName in properties) {
if([propertyName isEqualToString:@"mappingError"]) if([propertyName isEqualToString:@"mappingError"])
continue; continue;
NSString *mappedJSONKey; NSString *mappedJSONKey;
NSString *mappedJSONType; NSString *mappedJSONType;
NSString *propertyType = [properties valueForKey:propertyName]; NSString *propertyType = [properties valueForKey:propertyName];
id mappingValue = [[[self class] objcetMapping] valueForKey:propertyName]; id mappingValue = [[[self class] objcetMapping] valueForKey:propertyName];
if([mappingValue isKindOfClass:NSDictionary.class]) { if([mappingValue isKindOfClass:NSDictionary.class]) {
mappedJSONKey = [mappingValue valueForKey:PNObjectMappingKey]; mappedJSONKey = [mappingValue valueForKey:PNObjectMappingKey];
mappedJSONType = [mappingValue valueForKey:PNObjectMappingType]; mappedJSONType = [mappingValue valueForKey:PNObjectMappingType];
} else { } else {
mappedJSONKey = mappingValue; mappedJSONKey = mappingValue;
} }
/*if (fromLocal) { /*if (fromLocal) {
propertyName = mappedJSONKey; propertyName = mappedJSONKey;
}*/ }*/
if ([[PNObject protectedProperties] containsObject:propertyName] || [self isObjNull:mappedJSONKey]) { if ([[PNObject protectedProperties] containsObject:propertyName] || [self isObjNull:mappedJSONKey]) {
continue; continue;
} }
// Get JSON value for the mapped key // Get JSON value for the mapped key
id value; id value;
if (fromLocal) { if (fromLocal) {
@ -67,12 +67,12 @@
else { else {
value = [JSON valueForKeyPath:mappedJSONKey]; value = [JSON valueForKeyPath:mappedJSONKey];
} }
if([self isObjNull:value]) { if([self isObjNull:value]) {
continue; continue;
} }
((void (^)())@{ ((void (^)())@{
@"c" : ^{ @"c" : ^{
char val = [value charValue]; char val = [value charValue];
@ -106,11 +106,11 @@
if (![self isObjNull:value]) { if (![self isObjNull:value]) {
[self setValue:value forKey:propertyName]; [self setValue:value forKey:propertyName];
} }
}, },
@"NSNumber" : ^{ @"NSNumber" : ^{
NSInteger val = [value integerValue]; NSInteger val = [value integerValue];
[self setValue:@(val) forKey:propertyName]; [self setValue:@(val) forKey:propertyName];
}, },
@"NSDate" : ^{ @"NSDate" : ^{
@ -128,27 +128,37 @@
@"NSArray" : ^{ @"NSArray" : ^{
NSMutableArray *arr = [NSMutableArray array]; NSMutableArray *arr = [NSMutableArray array];
for(id JSONObject in value) { for(id JSONObject in value) {
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject fromLocal:fromLocal]; if([[JSONObject class] isSubclassOfClass:[PNObject class]]) {
[arr addObject:val]; PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject fromLocal:fromLocal];
[arr addObject:val];
}
else {
[arr addObject:JSONObject];
}
} }
[self setValue:arr forKey:propertyName]; [self setValue:arr forKey:propertyName];
}, },
@"NSMutableArray" : ^{ @"NSMutableArray" : ^{
NSMutableArray *arr = [NSMutableArray array]; NSMutableArray *arr = [NSMutableArray array];
for(id JSONObject in value) { for(id JSONObject in value) {
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject fromLocal:fromLocal]; if([[JSONObject class] isSubclassOfClass:[PNObject class]]) {
[arr addObject:val]; PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject fromLocal:fromLocal];
[arr addObject:val];
}
else {
[arr addObject:JSONObject];
}
} }
[self setValue:arr forKey:propertyName]; [self setValue:arr forKey:propertyName];
} }
}[propertyType] ?: ^{ }[propertyType] ?: ^{
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]]; BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) { if(isPNObjectSubclass) {
PNObject *val = [[NSClassFromString(propertyType) alloc] initWithJSON:value fromLocal:fromLocal]; PNObject *val = [[NSClassFromString(propertyType) alloc] initWithJSON:value fromLocal:fromLocal];
[self setValue:val forKey:propertyName]; [self setValue:val forKey:propertyName];
} }
@ -163,35 +173,35 @@
- (void)resetObject - (void)resetObject
{ {
NSDictionary *properties = [PNObject propertiesForClass:self.class]; NSDictionary *properties = [PNObject propertiesForClass:self.class];
for(NSString *propertyName in properties) { for(NSString *propertyName in properties) {
if([propertyName isEqualToString:@"mappingError"]) if([propertyName isEqualToString:@"mappingError"])
continue; continue;
NSString *mappedJSONKey; NSString *mappedJSONKey;
NSString *mappedJSONType; NSString *mappedJSONType;
NSString *propertyType = [properties valueForKey:propertyName]; NSString *propertyType = [properties valueForKey:propertyName];
id mappingValue = [self.JSONObjectMap valueForKey:propertyName]; id mappingValue = [self.JSONObjectMap valueForKey:propertyName];
if([mappingValue isKindOfClass:NSDictionary.class]) { if([mappingValue isKindOfClass:NSDictionary.class]) {
mappedJSONKey = [mappingValue valueForKey:@"key"]; mappedJSONKey = [mappingValue valueForKey:@"key"];
mappedJSONType = [mappingValue valueForKey:@"type"]; mappedJSONType = [mappingValue valueForKey:@"type"];
} else { } else {
mappedJSONKey = mappingValue; mappedJSONKey = mappingValue;
} }
if ([[PNObject protectedProperties] containsObject:propertyName] if ([[PNObject protectedProperties] containsObject:propertyName]
|| [propertyName isEqualToString:@"description"] || [propertyName isEqualToString:@"description"]
|| [propertyName isEqualToString:@"debugDescription"]) { || [propertyName isEqualToString:@"debugDescription"]) {
continue; continue;
} }
// Get JSON value for the mapped key // Get JSON value for the mapped key
((void (^)())@{ ((void (^)())@{
@"c" : ^{ @"c" : ^{
char val = '\0'; char val = '\0';
@ -251,34 +261,34 @@
- (NSDictionary* _Nonnull) getFormObject:(SEL _Nonnull) dictionaryMappingSelector - (NSDictionary* _Nonnull) getFormObject:(SEL _Nonnull) dictionaryMappingSelector
{ {
NSMutableDictionary *JSON = [NSMutableDictionary dictionary]; NSMutableDictionary *JSON = [NSMutableDictionary dictionary];
if ([[self class] respondsToSelector:dictionaryMappingSelector]) { if ([[self class] respondsToSelector:dictionaryMappingSelector]) {
NSDictionary *properties = [PNObject propertiesForClass:self.class]; NSDictionary *properties = [PNObject propertiesForClass:self.class];
NSDictionary *formMapping = [[self class] performSelector:dictionaryMappingSelector]; NSDictionary *formMapping = [[self class] performSelector:dictionaryMappingSelector];
for (NSString *formMappingKey in formMapping) { for (NSString *formMappingKey in formMapping) {
if ([[properties allKeys] containsObject:formMappingKey] && [properties objectForKey:formMappingKey]) { if ([[properties allKeys] containsObject:formMappingKey] && [properties objectForKey:formMappingKey]) {
id property = [self valueForKey:formMappingKey]; id property = [self valueForKey:formMappingKey];
id formMappingValue = [formMapping objectForKey:formMappingKey]; id formMappingValue = [formMapping objectForKey:formMappingKey];
NSString *mappedKey; NSString *mappedKey;
NSString *mappedType; NSString *mappedType;
NSDictionary *mappedValues; NSDictionary *mappedValues;
if ([formMappingValue isKindOfClass:[NSDictionary class]]) { if ([formMappingValue isKindOfClass:[NSDictionary class]]) {
mappedKey = [formMappingValue valueForKey:@"key"]; mappedKey = [formMappingValue valueForKey:@"key"];
mappedType = [formMappingValue valueForKey:@"type"]; mappedType = [formMappingValue valueForKey:@"type"];
mappedValues = [formMappingValue valueForKey:@"values"]; mappedValues = [formMappingValue valueForKey:@"values"];
} }
else { else {
mappedKey = formMappingKey; mappedKey = formMappingKey;
} }
((void (^)())@{ ((void (^)())@{
@"c" : ^{ @"c" : ^{
char val = [property charValue]; char val = [property charValue];
@ -308,14 +318,14 @@
BOOL val = [property boolValue]; BOOL val = [property boolValue];
[JSON setValue:@(val) forKey:mappedKey]; [JSON setValue:@(val) forKey:mappedKey];
}, },
@"UIImage" : ^{ @"UIImage" : ^{
UIImage *image = [UIImage imageWithData:property]; UIImage *image = [UIImage imageWithData:property];
[JSON setValue:image forKey:mappedKey]; [JSON setValue:image forKey:mappedKey];
}, },
@"NSURL" : ^{ @"NSURL" : ^{
NSURL *url = property; NSURL *url = property;
if (![self isObjNull:url]) { if (![self isObjNull:url]) {
[JSON setValue:[url absoluteString] forKey:mappedKey]; [JSON setValue:[url absoluteString] forKey:mappedKey];
} }
@ -339,29 +349,29 @@
@"NSArray" : ^{ @"NSArray" : ^{
NSMutableArray *arr = [NSMutableArray array]; NSMutableArray *arr = [NSMutableArray array];
for(id object in property) { for(id object in property) {
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]]; BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) { if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*) object getFormObject:dictionaryMappingSelector]; NSDictionary *objectDict = [(PNObject*) object getFormObject:dictionaryMappingSelector];
[arr addObject:objectDict]; [arr addObject:objectDict];
} }
} }
[JSON setValue:arr forKey:mappedKey]; [JSON setValue:arr forKey:mappedKey];
}, },
@"NSMutableArray" : ^{ @"NSMutableArray" : ^{
NSMutableArray *arr = [NSMutableArray array]; NSMutableArray *arr = [NSMutableArray array];
for(id object in property) { for(id object in property) {
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]]; BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) { if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*) object getFormObject:dictionaryMappingSelector]; NSDictionary *objectDict = [(PNObject*) object getFormObject:dictionaryMappingSelector];
[arr addObject:objectDict]; [arr addObject:objectDict];
} }
} }
[JSON setValue:arr forKey:mappedKey]; [JSON setValue:arr forKey:mappedKey];
} }
}[[property class]] ?: ^{ }[[property class]] ?: ^{
@ -395,15 +405,15 @@ static BOOL property_getTypeString( objc_property_t property, char *buffer )
const char * attrs = property_getAttributes( property ); const char * attrs = property_getAttributes( property );
if ( attrs == NULL ) if ( attrs == NULL )
return NO; return NO;
const char * e = strchr( attrs, ',' ); const char * e = strchr( attrs, ',' );
if ( e == NULL ) if ( e == NULL )
return NO; return NO;
int len = (int)(e - attrs); int len = (int)(e - attrs);
memcpy( buffer, attrs, len ); memcpy( buffer, attrs, len );
buffer[len] = '\0'; buffer[len] = '\0';
return YES; return YES;
} }
@ -414,13 +424,13 @@ static BOOL property_getTypeString( objc_property_t property, char *buffer )
if (PNObjClass == NULL) { if (PNObjClass == NULL) {
return nil; return nil;
} }
NSMutableDictionary *results = [NSMutableDictionary dictionary]; NSMutableDictionary *results = [NSMutableDictionary dictionary];
if ([PNObjClass isSubclassOfClass:[PNObject class]] && PNObjClass != [PNObject class]) { if ([PNObjClass isSubclassOfClass:[PNObject class]] && PNObjClass != [PNObject class]) {
[results addEntriesFromDictionary:[self propertiesForClass:class_getSuperclass(PNObjClass)]]; [results addEntriesFromDictionary:[self propertiesForClass:class_getSuperclass(PNObjClass)]];
} }
unsigned int outCount, i; unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(PNObjClass, &outCount); objc_property_t *properties = class_copyPropertyList(PNObjClass, &outCount);
for (i = 0; i < outCount; i++) { for (i = 0; i < outCount; i++) {
@ -434,7 +444,7 @@ static BOOL property_getTypeString( objc_property_t property, char *buffer )
} }
NSString *propertyName = [NSString stringWithUTF8String:propName]; NSString *propertyName = [NSString stringWithUTF8String:propName];
NSString *propertyType = [NSString stringWithUTF8String:propType]; NSString *propertyType = [NSString stringWithUTF8String:propType];
NSRange range = [propertyType rangeOfString:@"T@\""]; NSRange range = [propertyType rangeOfString:@"T@\""];
NSRange range2 = [propertyType rangeOfString:@"T"]; NSRange range2 = [propertyType rangeOfString:@"T"];
if (range.location != NSNotFound) { if (range.location != NSNotFound) {