字典转模型
来源:互联网 发布:乌克兰停电 网络攻击 编辑:程序博客网 时间:2024/06/06 01:46
//// Game.h// dictModelTrans//#import <Foundation/Foundation.h>@interface Game : NSObject@property(nonatomic,copy) NSString * name;@property(nonatomic,copy) NSString * userId; // ��id字段不可以使用,冲突@property(nonatomic,assign) BOOL isMember;- (instancetype)initWithDict:(NSDictionary*)dict;// 对象方法+ (instancetype)appWithDict:(NSDictionary*)dict; // 类方法/* id与instancetype的介绍 1. 在接收方法的返回值的时候id可以使用任何类型来接收, 编译都不报错, 但是运行时可能出错。 2. instancetype只能用作返回值类型, 不能向id一样声明变量、用作参数等 3. 使用instancetype, 编译器会检测instancetype的真实类型, 如果类型不匹配, 编译时就报错了。(instancetype出现在哪个类型中就表示对应的类型) 字典转模型 0. 字典转模型的含义: 把字典中的数据使用模型来保存。新建一个类, 根据字典中键值对的个数, 来编写这个类中的属性, 将来用这个类的对象的属性来保存字典中每个键对应的值。 * 字典缺陷: 1. 写代码的时候字典的键没有智能提示, 但是模型的属性可以有智能提示 2."键"是字符串, 如果写错了, 编译器不报错(在编译的时候不报错), 运行时可能出错, 出错了很难找错。 3. 使用"模型"可以更方便的使用面向对象的3大特(封装、继承、多态)性进行扩展。 */@end
//// Game.m// dictModelTrans//#import "Game.h"@implementation Game- (instancetype)initWithDict:(NSDictionary*)dict{ if (self = [super init]) { if (([dict objectForKey:@"name"])!=nil &&([dict objectForKey:@"name"])!=NULL ) { self.name = [self EncodeStringFromDic:dict andKey:@"name"]; } if (([dict objectForKey:@"id"])!=nil &&([dict objectForKey:@"id"])!=NULL ) { self.userId = [self EncodeStringFromDic:dict andKey:@"id"]; } if (([dict objectForKey:@"isMember"])!=nil &&([dict objectForKey:@"isMember"])!=NULL ) { self.isMember = [[dict objectForKey:@"isMember"] boolValue]; } } return self;}+ (instancetype)appWithDict:(NSDictionary*)dict{ return [[self alloc] initWithDict:dict];}-(NSString *)EncodeStringFromDic:(NSDictionary *)dic andKey:(NSString *)key{ if (nil != dic && [dic isKindOfClass:[NSDictionary class]]) { id temp = [dic objectForKey:key]; if ([temp isKindOfClass:[NSString class]]) { return temp; } else if ([temp isKindOfClass:[NSNumber class]]) { return [temp stringValue]; } } return @"";}@end
//// ViewController.m// dictModelTrans//// Created by 11 on 2016/11/15.// Copyright © 2016年 zg. All rights reserved.//#import "ViewController.h"#import "Game.h"@interface ViewController ()@property (nonatomic,strong) NSArray * gamesArray;@end@implementation ViewController// 懒加载-(NSArray *)gamesArray{ if (_gamesArray == nil) { NSString * path = [[NSBundle mainBundle] pathForResource:@"allGames" ofType:@"plist"]; NSArray * dictArray = [NSArray arrayWithContentsOfFile:path]; NSMutableArray * gamesArray = [NSMutableArray array]; for (NSDictionary * dict in dictArray) { Game * game = [Game appWithDict:dict]; [gamesArray addObject:game]; } _gamesArray = gamesArray; } return _gamesArray;}- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%lu",self.gamesArray.count); for (Game * game in self.gamesArray) { NSLog(@"%@",game.name); NSLog(@"%@",game.userId); NSLog(game.isMember?@"true":@"false"); }}@end
阅读全文
1 0
- 字典转模型时,字典包含字典
- 自定义模型转字典,字典转模型
- 字典转模型
- 字典转模型
- 字典转模型
- 字典转模型
- 字典转模型
- —字典转模型
- 字典转模型
- IOS字典转模型
- ios 字典转模型
- UI012---字典转模型
- 字典转模型
- 转载;字典转模型
- 字典转模型
- iOS 字典转模型
- 字典转模型
- 字典转模型简记
- 定时器
- 手机页面调用拨号的几种方法
- Java介绍和JDK配置
- Python连续攀升,其他的脚本语言去哪了?
- Oracle 分级查询
- 字典转模型
- let、const
- vue打包时报es6语法错误
- 6基于opencv的形态学处理(一)腐蚀_膨胀
- windows pdb
- C语言基础(一)
- flume到kafka动态topic
- chkconfig命令详解
- 均匀分布随机数的生成算法简介