#import <Foundation/Foundation.h>
#define MGInvalidServerDataException @"MGInvalidServerDataException"
@interface MGDataModel : NSObject
+(instancetype)modelObjectFromDictionary:(NSDictionary*)dictionary;
-(instancetype)initWithDictionary:(NSDictionary *)dictionary;
@end
#import "MGDataModel.h"
@implementation MGDataModel
+(instancetype)modelObjectFromDictionary:(NSDictionary *)dictionary
{
return [[[self class] alloc] initWithDictionary:dictionary];
}
-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
if ((self=[super init])&&![dictionary isKindOfClass:NSNull.class]) {
for (NSString* key in dictionary.keyEnumerator) {
id value=dictionary[key];
if (![value isKindOfClass:[NSNull class]]) {
SEL selector=NSSelectorFromString(key);
if ([self respondsToSelector:selector]&&![NSObject instancesRespondToSelector:selector]) {
@try {
[self setValue:dictionary[key] forKey:key];
} @catch (NSException *exception) {
}
}
}
}
}
return self;
}
@end