日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > 綜合技術(shù) > 用 KVC 自動(dòng)把 JSON 轉(zhuǎn) Model

用 KVC 自動(dòng)把 JSON 轉(zhuǎn) Model

來源:程序員人生   發(fā)布時(shí)間:2015-03-27 08:31:56 閱讀次數(shù):4190次


圖1和圖2是1個(gè)接口,code 是在服務(wù)器修改或升級(jí)等緣由致使的;圖3是在新用戶登錄沒有數(shù)據(jù)的情況出現(xiàn)的;是1個(gè)接口對(duì)應(yīng)的Model類也是1個(gè);Model類代碼以下

@interface SHYProduct : NSObject @property (nonatomic, assign) int code; @property (nonatomic, strong) NSString *msg; @property (nonatomic, strong) NSArray *data; @end @interface SHYProductItem : NSObject @property (nonatomic, strong) NSString *title; @end #import "SHYProduct.h" @implementation SHYProduct - (void)dealloc { _msg = nil; _data = nil; } @end @implementation SHYProductItem - (void)dealloc { _title = nil; } @end

之前我們?cè)谵D(zhuǎn)Model是這樣寫的

NSString *json = @"{"code":"200","msg":"u83b7u53d6u6210u529f","data":[{"title":"title 3"},{"title":"title 4"}]}"; NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *body = kNSDictionary([NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]); SHYProduct *product = [[SHYProduct alloc] init]; product.code = [body intForKey:@"code"]; product.msg = [body stringForKey:@"msg"]; NSArray *rows = [body arrayForKey:@"data"]; NSMutableArray *items = [[NSMutableArray alloc] init]; for (id row in rows) { NSDictionary *dictionary = kNSDictionary(row); SHYProductItem *item = [[SHYProductItem alloc] init]; item.title = [dictionary stringForKey:@"title"]; [items addObject:item]; } product.data = items;

這樣寫沒有甚么錯(cuò),唯1的是代碼大,體力活;

關(guān)于 intForKey 之類的方法請(qǐng)看 網(wǎng)絡(luò)接口協(xié)議 JSON 解析 Crash 的哪些事

如果我們不想做這個(gè)體力活;有無辦法呢;辦法是有1個(gè)的,用KVC + Runtime;在用這個(gè)之前我們要確認(rèn)1點(diǎn)用KVC code字段有數(shù)字和字符串能不能轉(zhuǎn)成int類型;是不是可以測試1下就知道;代碼以下:

NSString *json = @"{"code":"200","msg":"u83b7u53d6u6210u529f","data":[{"title":"title 3"},{"title":"title 4"}]}"; NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *body = kNSDictionary([NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]); SHYProduct *product = [[SHYProduct alloc] init]; [product setValue:[body valueForKey:@"code"] forKey:@"code"];//這里會(huì)轉(zhuǎn)成數(shù)字,KVC自動(dòng)轉(zhuǎn)換了 [product setValue:[body valueForKey:@"msg"] forKey:@"msg"]; NSArray *rows = [body arrayForKey:@"data"]; NSMutableArray *items = [[NSMutableArray alloc] init]; for (id row in rows) { NSDictionary *dictionary = kNSDictionary(row); SHYProductItem *item = [[SHYProductItem alloc] init]; [item setValue:[dictionary valueForKey:@"title"] forKey:@"title"]; [items addObject:item]; } product.data = items;

運(yùn)行1下code值過去了;JSON的code是數(shù)字和字符串最后都能變成int類型;這下好辦了寫1個(gè)Model基類就能夠替換體力活了;代碼以下:(開源代碼https://github.com/elado/jastor)

#import <Foundation/Foundation.h> /*! @class SHYJastor @abstract 把 NSDictionary 轉(zhuǎn)成 model 用的 */ @interface SHYJastor : NSObject<NSCoding> /*! @property objectId @abstract 對(duì)象的id */ @property (nonatomic, copy) NSString *objectId; /*! @method objectWithDictionary: @abstract 指定 NSDictionary 對(duì)象轉(zhuǎn)成 model 對(duì)象 @param dictionary NSDictionary的對(duì)象 @result 返回 model 對(duì)象 */ + (id)objectWithDictionary:(NSDictionary *)dictionary; /*! @method initWithDictionary: @abstract 指定 NSDictionary 對(duì)象轉(zhuǎn)成 model 對(duì)象 @param dictionary NSDictionary的對(duì)象 @result 返回 model 對(duì)象 */ - (id)initWithDictionary:(NSDictionary *)dictionary; /*! @method dictionaryValue @abstract 把對(duì)象轉(zhuǎn)成 NSDictionary 對(duì)象 @result 返回 NSDictionary 對(duì)象 */ - (NSMutableDictionary *)dictionaryValue; /*! @method mapping @abstract model 屬性 與 NSDictionary 不1至?xí)r的映照 */ - (NSDictionary *)mapping; @end #import "SHYJastor.h" #import "SHYJastorRuntimeHelper.h" #import "NSArray+SHYUtil.h" #import "NSDictionary+SHYUtil.h" static NSString *idPropertyName = @"id"; static NSString *idPropertyNameOnObject = @"objectId"; @implementation SHYJastor Class dictionaryClass; Class arrayClass; + (id)objectWithDictionary:(NSDictionary *)dictionary { id item = [[self alloc] initWithDictionary:dictionary]; return item; } - (id)initWithDictionary:(NSDictionary *)dictionary { if (!dictionaryClass) dictionaryClass = [NSDictionary class]; if (!arrayClass) arrayClass = [NSArray class]; self = [super init]; if (self) { NSDictionary *maps = [self mapping]; NSArray *propertys = [SHYJastorRuntimeHelper propertyNames:[self class]]; for (NSDictionary *property in propertys) { NSString *propertyName = [property stringForKey:@"name"]; id key = [maps valueForKey:propertyName]; id value = [dictionary valueForKey:key]; if (value == [NSNull null] || value == nil) { continue; } if ([SHYJastorRuntimeHelper isPropertyReadOnly:[property stringForKey:@"attributes"]]) { continue; } if ([value isKindOfClass:dictionaryClass]) { Class aClass = NSClassFromString([property stringForKey:@"type"]); if (![aClass isSubclassOfClass:[NSDictionary class]]) { continue; } value = [[aClass alloc] initWithDictionary:value]; } else if ([value isKindOfClass:arrayClass]) { NSArray *items = (NSArray *)value; NSMutableArray *objects = [NSMutableArray arrayWithCapacity:[items count]]; for (id item in items) { if ([[item class] isSubclassOfClass:dictionaryClass]) { SEL selector = NSSelectorFromString([NSString stringWithFormat:@"%@Class", propertyName]); #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" Class aClass = ([[self class] respondsToSelector:selector]) ? [[self class] performSelector:selector] : nil; #pragma clang diagnostic pop if ([aClass isSubclassOfClass:[NSDictionary class]]) { [objects addObject:item]; } else if ([aClass isSubclassOfClass:[SHYJastor class]]) { SHYJastor *childDTO = [[aClass alloc] initWithDictionary:item]; [objects addObject:childDTO]; } } else { [objects addObject:item]; } } value = objects; } [self setValue:value forKey:propertyName]; } id objectId; if ((objectId = [dictionary objectForKey:idPropertyName]) && objectId != [NSNull null]) { if (![objectId isKindOfClass:[NSString class]]) { objectId = [NSString stringWithFormat:@"%@", objectId]; } [self setValue:objectId forKey:idPropertyNameOnObject]; } } return self; } - (void)dealloc { _objectId = nil; } - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:_objectId forKey:idPropertyNameOnObject]; NSArray *propertys = [SHYJastorRuntimeHelper propertyNames:[self class]]; for (NSDictionary *property in propertys) { NSString *propertyName = [property stringForKey:@"name"]; [encoder encodeObject:[self valueForKey:propertyName] forKey:propertyName]; } } - (id)initWithCoder:(NSCoder *)decoder { self = [super init]; if (self) { [self setValue:[decoder decodeObjectForKey:idPropertyNameOnObject] forKey:idPropertyNameOnObject]; NSArray *propertys = [SHYJastorRuntimeHelper propertyNames:[self class]]; for (NSDictionary *property in propertys) { NSString *propertyName = [property stringForKey:@"name"]; if ([SHYJastorRuntimeHelper isPropertyReadOnly:[property stringForKey:@"attributes"]]) { continue; } id value = [decoder decodeObjectForKey:propertyName]; if (value != [NSNull null] && value != nil) { [self setValue:value forKey:propertyName]; } } } return self; } - (NSMutableDictionary *)dictionaryValue { NSMutableDictionary *infos = [NSMutableDictionary dictionary]; if (_objectId) { [infos setObject:_objectId forKey:idPropertyName]; } NSDictionary *maps = [self mapping]; NSArray *propertys = [SHYJastorRuntimeHelper propertyNames:[self class]]; for (NSDictionary *property in propertys) { NSString *propertyName = [property stringForKey:@"name"]; id value = [self valueForKey:propertyName]; if (value && [value isKindOfClass:[SHYJastor class]]) { [infos setObject:[value dictionary] forKey:[maps valueForKey:propertyName]]; } else if (value && [value isKindOfClass:[NSArray class]] && ((NSArray *)value).count > 0) { id internalValue = [value objectForKeyCheck:0]; if (internalValue && [internalValue isKindOfClass:[SHYJastor class]]) { NSMutableArray *internalItems = [NSMutableArray array]; for (id item in value) { [internalItems addObject:[item dictionary]]; } [infos setObject:internalItems forKey:[maps valueForKey:propertyName]]; } else { [infos setObject:value forKey:[maps valueForKey:propertyName]]; } } else if (value != nil) { [infos setObject:value forKey:[maps valueForKey:propertyName]]; } } return infos; } - (NSDictionary *)mapping { NSArray *properties = [SHYJastorRuntimeHelper propertyNames:[self class]]; NSMutableDictionary *maps = [[NSMutableDictionary alloc] initWithCapacity:properties.count]; for (NSDictionary *property in properties) { NSString *propertyName = [property stringForKey:@"name"]; [maps setObject:propertyName forKey:propertyName]; } return maps; } - (NSString *)description { NSMutableDictionary *dictionary = [self dictionaryValue]; return [NSString stringWithFormat:@"#<%@: id = %@ %@>", [self class], _objectId, [dictionary description]]; } - (BOOL)isEqual:(id)object { if (object == nil || ![object isKindOfClass:[SHYJastor class]]) { return NO; } SHYJastor *model = (SHYJastor *)object; return [_objectId isEqualToString:model.objectId]; } @end @interface SHYJastorRuntimeHelper : NSObject + (BOOL)isPropertyReadOnly:(NSString *)attributes; + (NSArray *)propertyNames:(__unsafe_unretained Class)aClass; @end #import <objc/runtime.h> #import "SHYJastor.h" #import "SHYJastorRuntimeHelper.h" #import "NSArray+SHYUtil.h" #import "NSDictionary+SHYUtil.h" #include <string.h> static NSMutableDictionary *propertyListByClass; static const char *property_getTypeName(const char *attributes) { char buffer[strlen(attributes) + 1]; strncpy(buffer, attributes, sizeof(buffer)); char *state = buffer, *attribute; while ((attribute = strsep(&state, ",")) != NULL) { if (attribute[0] == 'T') { size_t len = strlen(attribute); attribute[len - 1] = ' 主站蜘蛛池模板: 亚洲成人高清 | 美女被免费喷白浆视频 | 91精品成人久久 | 久久久久成人网 | 久久久久免费 | 天天综合天天做天天综合 | 欧美三级三级三级爽爽爽 | 激情欧美日韩一区二区 | 久久精品成人一区二区三区蜜臀 | 欧美日韩在线视频一区二区 | 日韩欧美中文字幕在线视频 | 国产一区二区欧美精品 | 国产精品一区二区在线播放 | 日韩在线三区 | 国产精品久久久久久久第一福利 | 久久国产在线观看 | 精品国产一区二区三区成人影院 | 91精品国产一区二区 | 91免费网站 | 久久精品91| 国产一级黄大片 | 成人在线观看av | 国产成人精品免高潮在线观看 | 久久久久久国产 | 欧美日韩国产高清视频 | 精品一区二区久久久久久久网站 | 国产一区福利 | 国产黄色在线网站 | 成人性爱视频在线观看 | 国产精品卡一卡二 | 国产精品久久av | 久久都是精品 | 午夜视频在线观看网站 | 91精品国产综合久久久久 | 日韩精品一区二区在线观看 | 国产麻豆久久 | 久久av在线 | 欧美精品一区在线 | 国产精品久久久久久久久久免费 | 国产福利二区 | 九一网站在线观看 |