6.ios之字典与模型
1.用模型取代字典的好处
?使用字典的坏处
dict[@"name"] =@"Jack";
NSString*name = dict[@"name"];
app.name = @"Jack”;
NSString*name = app.name;
2.字典转模型
4.字典转模型的过程
5.Sample
#import<Foundation/Foundation.h> @interface MJApp :NSObject -(NSString*)GetName; -(NSString*)GetIcon; -(instancetype) initWithDict:(NSDictionary *) dict; +(instancetype) appWithDict:(NSDictionary *) dict; @end #import "MJApp.h" @interface MJApp() @property (nonatomic,copy) NSString *name; @property (nonatomic,copy) NSString *icon; @end @implementation MJApp -(NSString*)GetName { return self.name; } -(NSString*)GetIcon { return self.icon; } -(instancetype) initWithDict:(NSDictionary *)dict { if (self = [super init]) { self.name = dict[@"name"]; self.icon = dict[@"icon"]; } return self; } +(instancetype)appWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。