BaseModel的简单使用
Model主要作用便是为不同的数据创建对象,然后用于处理。我这里使用上只用到两个方面
1. 将服务器返回的数据转换成对象model,方便开发时的使用和保存。
2. 将一些和数据相关的操作在model中进行处理。
1. 数据转为model对象
这个实现上比较简单,原理就是键值对映射处理。已经写好了一个BaseModel,使用时只需要继承这个BaseModel即可。
@interface BaseModel : NSObject
//@property (nonatomic,copy)NSString *ret;//结果代码 1成功 0接口验证失败 -1渠道编码不存在
//@property (nonatomic,copy)NSString *tip;//结果描述
-(id)initWithDataDic:(NSDictionary*)data;//将字典转换为model
- (void)setAttributes:(NSDictionary*)dataDic;//键值对映射
- (NSDictionary*)attributeMapDictionary;
- (NSString *)customDescription;
- (NSString *)description;
- (NSData*)getArchivedData;
- (NSString *)cleanString:(NSString *)str; //清除\n和\r的字符串
- (NSDictionary *)convertToDictionary;//转换为字典
@end
实现操作流程如下
#import "MessageViewController.h"
#import "MessageModel.h"
@interface MessageViewController ()
@end
@implementation MessageViewController
- (void)viewDidLoad