Skip to content

Instantly share code, notes, and snippets.

@ipconfiger
Created January 16, 2014 16:31
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ipconfiger/8458066 to your computer and use it in GitHub Desktop.
映射json反序列化的字典到自定义类型
//
// BaseModle.m
// apitester
//
// Created by LiMing on 14-1-16.
// Copyright (c) 2014年 LiMing. All rights reserved.
//
#import "BaseModle.h"
#import <objc/message.h>
@implementation BaseModle
-(id)init:(NSDictionary *)data{
self = [super init];
if (self){
[self setAttributes:data];
}
return (self);
}
- (id)copyWithZone:(NSZone *)zone {
id bmodel = [[self.class allocWithZone:zone] init];
return bmodel;
}
-(void) loadData:(NSDictionary*)data{
[self setAttributes:data];
}
- (NSDictionary *)attributeMapDictionary{
NSMutableDictionary *propertyDict = [[NSMutableDictionary alloc] init];
unsigned int outCount = 0;
objc_property_t *properties = class_copyPropertyList(self.class, &outCount);
for (int i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
NSString *propName = [NSString stringWithUTF8String:property_getName(property)];
const char *typeName =property_getAttributes(property);
NSString *properyTypeName = [[NSString alloc] initWithCString:typeName encoding:NSUTF8StringEncoding];
//NSLog(@"property class is %@", properyTypeName);
[propertyDict setObject:properyTypeName forKey:propName];
}
return propertyDict;
}
- (void)setAttributes:(NSDictionary *)data{
NSDictionary *propertys = [self attributeMapDictionary];
for (NSString *attributeName in [propertys allKeys]){
SEL sel = [self getSetterAttributeName:attributeName];
if ([self respondsToSelector:sel]) {
id value = [data objectForKey:attributeName];
if (value) {
NSString* className = [self className:[propertys objectForKey:attributeName]];
if ([value isKindOfClass:[NSDictionary class]]) {
id subObject = [[NSClassFromString(className) alloc] init:value];
[self performSelectorOnMainThread:sel withObject: subObject waitUntilDone:YES];
continue;
}
if ([value isKindOfClass:[NSArray class]]) {
if ([className isEqualToString:@"NSString"] || [className isEqualToString:@"NSNumber"]) {
[self performSelectorOnMainThread:sel withObject: value waitUntilDone:YES];
continue;
}
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
for (id dateItem in value) {
id subObject = [[NSClassFromString(className) alloc] init:dateItem];
[dataArray addObject:subObject];
}
[self performSelectorOnMainThread:sel withObject: dataArray waitUntilDone:YES];
continue;
}
[self performSelectorOnMainThread:sel withObject:value waitUntilDone:YES];
}
}
}
}
- (NSString*) className:(NSString *)propertyTypeName {
NSLog(@"%@", propertyTypeName);
NSString* name = [[propertyTypeName componentsSeparatedByString:@","] objectAtIndex:0];
NSString* cName = [[name substringToIndex:[name length]-1] substringFromIndex:3];
if ([cName rangeOfString:@"<"].location != NSNotFound) {
NSString* subName = [cName substringFromIndex:[cName rangeOfString:@"<"].location+1];
return [subName substringToIndex:[subName length]-1];
}
return cName;
}
- (SEL)getSetterAttributeName:(NSString *)attributeName
{
NSString *capital = [[attributeName substringToIndex:1] uppercaseString];
NSString *setterSelStr = [NSString stringWithFormat:@"set%@%@:",capital,[attributeName substringFromIndex:1]];
return NSSelectorFromString(setterSelStr);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment