2018年5月31日
一.两个接口类型用同一个model接收,导致一个接口对应显示页面正常显示,一个接口对应的页面显示不正常。
1.原因:类型转换了导致
老的接口返回正常。
之后新加了一种type,新的接口,返回的是如下字段title startTime endTime; 想简单起见,用如下方式转换。
结果现象是新的type(分组是对的),导致老的类型培训 显示都为空。【因为老接口,title没有返回,虽然有courseName返回,也被转成nil了】。
3.正确的做法,字段还是不要转化,model可以添加新的类型,在获取数据后,在根据type给新的type复制新值。
2017年8月23日
1.回来的数据在修改
if (data != nil && ![data isKindOfClass:[NSNull class]]) {
for (NSDictionary *dict in data){
newMessModel *model = [newMessModel mj_objectWithKeyValues:dict];
model.subNo = HuSubSysTypeNT; //默认是护士培训
if(model){
[dataArray addObject:model];
}
}
}
2017年8月10日
1.常见问题
如下,这么简单的解析都不成功
原因:cell重画接口 又调用了模型解析,导致多个解析串行,进而导致解析不正常。
解决:去掉不合理的cell 层model 解析
2017年8月9日
1.模型数组-字典数组
//模型数组-字典数组
NSArray<HuTrainKindModel *> *dataArray = [HuTrainKindModel mj_objectArrayWithKeyValuesArray:value];
NSArray *dicArray = [HuTrainKindModel mj_keyValuesArrayWithObjectArray:dataArray];
[HuConfigration writeConfigWithKey:key WithValue:dicArray];
2.字典数组-模型数组
NSArray *dicArray = [HuConfigration readConfigWithKey:discoverHomePageConfigureKey];
//字典数组-模型数组
NSArray<HuTrainKindModel *> *tempArr = [HuTrainKindModel mj_objectArrayWithKeyValuesArray:dicArray];
3.字典转模型,模型里面含有模型
模型里面还有模型也可以直接转
@interface HuDiscoveryModel : NSObject<MJKeyValue>
@property (nonatomic, strong) HuImageModel *zwDescr; //中卫描述
@property (nonatomic, strong) HuImageModel *recommendExpert;//推荐专家
@property (nonatomic, strong) NSArray<HuExpertClassModel *> *recommendCourse;//推荐课程
@end
weakSelf.disoveryModel = [HuDiscoveryModel mj_objectWithKeyValues:dic[@"data"]];
NSLog(@"wy=url=%@",weakSelf.disoveryModel.zwDescr.imgUrl);
2017年5月22日
1.数据操作
weakSelf.myList = [TrainTestListModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"result"]];
2017年4月9日
1.dic to json 会奔溃,目前只能用右边的方法
2017年4月8日
1.model转换成dic
param = _reqParamM.mj_keyValues;
2017年3月4日
1.将dic装换成模型
@interface NSDictionary (NIMKitExtension)
- (NSString *)nimkit_jsonString;
@end
@implementation NSDictionary (NIMKitExtension)
- (NSString *)nimkit_jsonString
{
NSData *data = [NSJSONSerialization dataWithJSONObject:self
options:0
error:nil];
return data ? [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] : nil;
}
@end
- (void)uploadPaper
{
//补全其他数据
_uploadUserAnsM.releaseId = @"1";
_uploadUserAnsM.accountId = @"1";
_uploadUserAnsM.courseId = @"1";
_uploadUserAnsM.contentId = @"1";
_uploadUserAnsM.courseName = @"test";//self.courseName;
_uploadUserAnsM.accountName = NurseName;
//把model 转换成 dic 上传
NSDictionary *temp = _uploadUserAnsM.mj_keyValues;
NSDictionary *param =@{@"data":[temp nimkit_jsonString]};
NSString *funItem = HuFuncItem_doPaper;
WS(weakSelf);
[Request requestAppServerNT:param funItem:funItem success:^(NSDictionary *dic) {
//答题成功后进入分析界面
HuResExerciseModel *resExerciseModel = [HuResExerciseModel mj_objectWithKeyValues: dic[@"data"]];
// [weakSelf enterTestResultPage:resExerciseModel];
// 提示 在跳转到详情即可
NSString *message = [NSString stringWithFormat:@"全部%@题,正确%@题",resExerciseModel.total,resExerciseModel.successCount];
[MBProgressHUD showMessageInCurrentWindow:message];
[weakSelf backClick];
} error:^(NSDictionary *dic) {
TRACELOG(@"%@",dic[@"errMsg"] ? dic[@"errMsg"] : @"网络异常");
} failure:^{
TRACELOG(@"failure");
}];
}
2017年3月2日
1.将dic类型数据转换成模型
dic类型含有模型数组,dic的返回的key和model的属性值可以不一致。
场景:
将习题dic转换成对应的model
{
"data": {
"createId": 1,
"createTime": "2017-02-24 12:00:00",
"exerciseRspDTOList": [
{
"createId": 1,
"createTime": "2017-02-24 12:00:00",
"exerciseItemRspDOList": [
{
"exerciseId": 1,
"hiddenNo": 1,
"id": 1,
"name": "这是第 1 题,第 1 个选项",
"no": "A",
"paperId": 1,
"validFlag": "ENABLE"
}
],
"hiddenNo": 1,
"id": 1,
"name": "这是第1题,请选择正确答案",
"no": "1",
"paperId": 1,
"score": 10,
"type": 1,
"updateId": 0,
"validFlag": "ENABLE"
}
],
"id": 1,
"passScore": 60,
"totalScore": 100,
"updateId": 0,
"validFlag": "ENABLE"
},
"errMsg": "操作成功",
"success": true
}
实现:
第一步 定义model (设置替换值和数组模型)【关键是替换的数组模型的key,需要用model里面的key】
// HuExercisesModel.h
#import <Foundation/Foundation.h>
@class HuExercise,HuChoice;
@interface HuExercisesModel : NSObject
@property (nonatomic, strong) NSString *createId;
@property (nonatomic, strong) NSString *createTime;
@property (nonatomic, strong) NSArray<HuExercise *> *exercises;
@property (nonatomic, strong) NSString *ID;
@property (nonatomic, strong) NSString *passScore;
@property (nonatomic, strong) NSString *totalScore;
@property (nonatomic, strong) NSString *updateId;
@property (nonatomic, strong) NSString *validFlag;
@end
@interface HuExercise : NSObject<MJKeyValue>
@property (nonatomic, strong) NSString *createId;
@property (nonatomic, strong) NSString *createTime;
@property (nonatomic, strong) NSArray<HuChoice *> *choices; //习题选项
@property (nonatomic, strong) NSString *hiddenNo;
@property (nonatomic, strong) NSString *ID; //习题id
@property (nonatomic, strong) NSString *name;//习题题目
@property (nonatomic, strong) NSString *showId;//显示的习题编号
@property (nonatomic, strong) NSString *paperId;
@property (nonatomic, strong) NSString *score;
@property (nonatomic, assign) NSInteger type; //习题类型
@property (nonatomic, strong) NSString *updateId;
@property (nonatomic, strong) NSString *validFlag;
@property (nonatomic, strong) NSString *answer; //习题答案
//测试成绩界面用到的
@property (nonatomic, strong) NSString *userAnswer;//用户答案,在测试成绩界面用到
@property (nonatomic, assign) NSInteger numId;//内部使用,(1,2,3,4,...) 用来定位当前在测试成绩界面第几题
@property (nonatomic, assign) BOOL isRight;//答题是否正确
@end
// HuExercisesModel.m
#import "HuExercisesModel.h"
@implementation HuExercisesModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
return @{@"exercises": @"exerciseRspDTOList",
@"ID": @"id"
};
}
+ (NSDictionary *)mj_objectClassInArray {
return @{@"exercises": [HuExercise class]}; //注意这个不是dic返回的exerciseRspDTOList值
}
@end
@implementation HuExercise
+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
return @{@"choices": @"exerciseItemRspDOList",
@"ID": @"id",
@"showId": @"no"
};
}
+ (NSDictionary *)mj_objectClassInArray {
return @{@"choices": [HuChoice class]}; //注意这个不是dic返回的exerciseItemRspDOList值
}
@end
@implementation HuChoice
+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
return @{@"eId": @"exerciseId",
@"ID": @"id",
@"showId": @"no"
};
}
@end
第二步使用:如下一步解析既可使用
[Request requestAppServerNT:param funItem:funItem success:^(NSDictionary *dic) {
self.exercisesM = [HuExercisesModel mj_objectWithKeyValues: dic[@"data"]];
NSLog(@"hhe");
}
效果如下:
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。