Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active February 27, 2016 13:04
Show Gist options
  • Select an option

  • Save lamprosg/abfc06894d066f9b0758 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/abfc06894d066f9b0758 to your computer and use it in GitHub Desktop.
(iOS) - JSON parsing snippets
//Init a class model from a NSDictionary
- (id)initWithDictionary:(NSDictionary*)dictionary {
if(!dictionary || [dictionary isKindOfClass:[NSNull class]]) {
return nil;
}
self = [super init];
if(self) {
// TODO: check if classes can be readily instatiated too.
NSDictionary *classProps = [RSBaseModel classPropsFor:[self class]];
if(classProps.allKeys.count == 0) {
NSLog(@"Class %@ has no properties.", [self class]);
return nil;
}
for(NSString *propertyName in [self propertyNames]) {
if([classProps[propertyName] isEqualToString:@"NSDate"]) {
// Initialize the dates based on the received string.
// Maybe it is not needed, but I have to check it.
NSString *dateString = dictionary[propertyName];
NSDate *date;
if(![dateString isKindOfClass:[NSNull class]]) {
date = [dateString toFullDate];
}
else {
date = nil;
}
[self setValue:date forKey:propertyName];
}
else {
NSString *strippedPropertyName = [self excludeUnderscorePrefix:propertyName];
if(strippedPropertyName) {
id value = dictionary[strippedPropertyName];
if(!value) {
value = [[NSClassFromString(classProps[propertyName]) alloc] init];
}
if(![value isKindOfClass:[NSNull class]]) {
[self setValue:value forKey:propertyName];
}
}
}
}
}
return self;
}
- (NSArray*)propertyNames {
unsigned int propertyCount = 0;
objc_property_t *properties = class_copyPropertyList([self class], &propertyCount);
NSMutableArray *propertyNames = [NSMutableArray array];
for (unsigned int i = 0; i < propertyCount; ++i) {
objc_property_t property = properties[i];
const char * name = property_getName(property);
[propertyNames addObject:[NSString stringWithUTF8String:name]];
}
free(properties);
return propertyNames;
}
//Convert an array of dictionaries in a NSArray of model classes
-(NSArray*)getModelArrayForClassNamed:(NSString*)className
withJSONArray:(NSArray*)jsonArray {
if([jsonArray isKindOfClass:[NSNull class]]) {
return nil;
}
NSMutableArray *finalArray = [[NSMutableArray alloc] initWithCapacity:10];
for(NSDictionary *dict in jsonArray) {
id item = [[NSClassFromString(className) alloc] initWithDictionary:dict];
[finalArray addObject:item];
}
return (NSArray*)finalArray;
}
-(NSDictionary*) getJsonFromFileWithPath:(NSString*)path {
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *resource = [bundle pathForResource:path ofType:@"json"];
NSData *myData = [NSData dataWithContentsOfFile:resource];
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:myData options:kNilOptions error:&error];
return json;
}
-(UIColor *)colorWithHexString:(NSString *)stringToConvert
{
@try {
NSString *noHashString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""]; // remove the #
NSScanner *scanner = [NSScanner scannerWithString:noHashString];
[scanner setCharactersToBeSkipped:[NSCharacterSet symbolCharacterSet]]; // remove + and $
unsigned hex;
if (![scanner scanHexInt:&hex]) return nil;
int r = (hex >> 16) & 0xFF;
int g = (hex >> 8) & 0xFF;
int b = (hex) & 0xFF;
return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f];
}
@catch (NSException *exception) {
return nil;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment