BaseResponse.m
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// BaseResponse.m
// IM_ios_client
//
// Created by 赵世强 on 16/1/4.
// Copyright © 2016年 xiniu. All rights reserved.
//
#import "BaseResponse.h"
@implementation BaseResponse
@synthesize fields;
@synthesize errors;
static NSString * const f_errors = @"errors";
static NSString * const f_code = @"code";
static NSString * const f_message = @"message";
static NSString * const f_type = @"type";
static NSString * const f_solution = @"solution";
static NSString * const f_totalCount = @"totalCount";
- (instancetype)init
{
self = [super init];
if (self) {
fields = [[NSMutableDictionary alloc]init];
errors = [[NSArray alloc]init];
}
return self;
}
// 解析json生成数据字典
-(BaseResponse *)analyzeJson:(NSString *)responseString
{
if (responseString) {
//fields = [responseString objectFromJSONString];
fields = [self f_jsonDataToDic:[responseString dataUsingEncoding:NSUTF8StringEncoding] option:NSJSONReadingMutableContainers];
}
/*
NSLog(@"%@",[fields class]);
NSLog(@"%lu",(unsigned long)fields.count);
NSArray *arr = [fields allKeys];
for (int i = 0; i < arr.count; i++) {
//NSLog(@"%@",[[dic objectForKey:[arr objectAtIndex:i]] class]);
if ([[fields objectForKey:[arr objectAtIndex:i]] isKindOfClass:[NSArray class]])
{
NSLog(@"array");
}
else if([[fields objectForKey:[arr objectAtIndex:i]] isKindOfClass:[NSDictionary class]])
{
NSLog(@"dictionary");
NSDictionary *dictemp = [fields objectForKey:[arr objectAtIndex:i]];
NSArray *arrtemp = [dictemp allKeys];
for (int j = 0; j < arrtemp.count; j++) {
//NSLog(@"user中成员%@类型:%@",[arrtemp objectAtIndex:j],[[dictemp objectForKey:[arrtemp objectAtIndex:j]] class]);
NSLog(@"user中成员%@值:%@",[arrtemp objectAtIndex:j],[dictemp objectForKey:[arrtemp objectAtIndex:j]]);
}
}else{
NSLog(@"key/value");
}
}
//userDomain = [UserDomain new];
//[userDomain initWithDic:dic];
//NSLog(@"%@",userDomain.account);
*/
return self;
}
#pragma getter/setter
-(NSArray *)errors
{
return (NSArray *)[fields objectForKey:f_errors];
}
-(void)setErrors:(NSArray *)errors
{
}
-(NSString *)code
{
NSArray *arr = (NSArray *)[fields objectForKey:f_errors];
if (arr.count > 0) {
NSDictionary *dic = [arr objectAtIndex:0];
return [dic objectForKey:f_code];
}else{
return [fields objectForKey:f_code];
}
}
-(void)setCode:(NSString *)code
{
}
-(NSString *)message
{
NSArray *arr = (NSArray *)[fields objectForKey:f_errors];
if (arr.count > 0) {
NSDictionary *dic = [arr objectAtIndex:0];
return [dic objectForKey:f_message];
}else{
return [fields objectForKey:f_message];
}
}
-(void)setMessage:(NSString *)message
{
}
-(NSString *)solution
{
return [fields objectForKey:f_solution];
}
-(void)setSolution:(NSString *)solution
{
}
-(NSString *)type
{
NSArray *arr = (NSArray *)[fields objectForKey:f_errors];
if (arr.count > 0) {
NSDictionary *dic = [arr objectAtIndex:0];
return [dic objectForKey:f_type];
}else{
return [fields objectForKey:f_type];
}
}
-(void)setType:(NSString *)type
{
}
-(long)totalCount
{
return [[fields objectForKey:f_totalCount] integerValue];
}
-(void)setTotalCount:(long)totalCount
{
}
#pragma mark - 共同的设置Domain元素的方法
/*
itemObj:元素值
itemKey:元素的值在元素所属Domain的字典中对应的Key
selfFields:元素所属Domain的字典
selfKey:元素所属Domain的字典在WKBaseDomain中的Key
*/
-(void)setItemObj:(id)itemObj itemKey:(id<NSCopying>)itemKey selfFields:(NSMutableDictionary *)selfFields selfKey:(id<NSCopying>)selfKey
{
[selfFields setObject:itemObj forKey:itemKey];
[fields setObject:selfFields forKey:selfKey];
}
#pragma mark - 根据Domain的字典在WKBaseDomain中的Key获得Domain的字典
-(NSMutableDictionary *)getDomainDicByKey:(NSString *)domainKey
{
NSMutableDictionary *dic_self = [[NSMutableDictionary alloc]init];
if ([fields objectForKey:domainKey]) {
dic_self = [NSMutableDictionary dictionaryWithDictionary:[fields objectForKey:domainKey]];
}
return dic_self;
}
#pragma mark - 判读字典中是否存在某个key
-(BOOL)keyExistInDictionary:(NSMutableDictionary *)dic key:(NSString *)key
{
if (![dic isKindOfClass:[NSNull class]] && dic.allKeys.count > 0 && [[dic allKeys] containsObject:key]) {
return YES;
}
return NO;
}
#pragma mark jsonString转为NSMutableDictionary(NSJSONSerialization解析)
/**
* @author qianjun
*
* @brief jsonString转为NSMutableDictionary(NSJSONSerialization解析)
*
* @param theData dic/array
*
* @return jsonString
*/
- (NSMutableDictionary *)f_jsonDataToDic:(NSData*)jsonData option:(NSJSONReadingOptions)option
{
NSError* error;
if(jsonData)
{
NSMutableDictionary *muDic_json = [NSJSONSerialization
JSONObjectWithData:jsonData
options:option error:&error];
if(muDic_json && muDic_json.count > 0)
{
return muDic_json;
}
}
return nil;
}
@end