LoginModel.h
#import <Foundation/Foundation.h>
@interface LoginModel : NSObject
@property (copy, nonatomic) NSString *memberID;
@property (copy, nonatomic) NSString *area;
@property (copy, nonatomic) NSString *cityName;
@property (assign, nonatomic) NSInteger memberLevel;
- (void)loginWithParameter:(NSDictionary *)parameters success:(void(^)(NSString *successMsg))success failure:(void(^)(NSString *failureMsg))failure;
@end
LoginModel.m
- (void)loginWithParameter:(NSDictionary *)parameters success:(void (^)(NSString *))success failure:(void (^)(NSString *))failure{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:@"http://musicbox.jidait.com/WebApi/Member/Login" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id result = [responseObject objectForKey:@"result"];
if ([[result objectForKey:@"success"] boolValue]) {
if (success) {
success(@"登陆成功");
}
NSDictionary *entity = [responseObject objectForKey:@"entity"];
}
else{
if (failure) {
failure(@"登陆失败");
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure([error localizedDescription]);
}
}];
}
说明:
山面代码中的entity就是获取的json串。
对json的处理:
通过访问器:
NSDictionary *entity = [responseObject objectForKey:@"entity"];
[self map:entity];
- (void)map:(NSDictionary *)entity{
self.memberID = [self filtereDictionary:entity ForKey:@"MemberID"];
self.area = [self filtereDictionary:entity ForKey:@"Area"];
self.cityName = [self filtereDictionary:entity ForKey:@"CityName"];
self.memberLevel = [[self filtereDictionary:entity ForKey:@"MemberLevel"] integerValue];
}
/*处理三种情况:
1.json中对应key不存在,那么程序抛出异常。
2.key对应的值为[NSNull null],那么返回nil;
*/
- (id)filtereDictionary:(NSDictionary *)entity ForKey:(NSString *)key{
id value = [entity objectForKey:key];
if (!value) {
NSException *exception = [NSException exceptionWithName:@"警告" reason:@"字典中没有对应的Key" userInfo:nil];
@throw exception;
}
if ([value isEqual:[NSNull null]]) {
return nil;
}
return value;
}
通过kvc:
NSDictionary *entity = [responseObject objectForKey:@"entity"];
[self setValuesForKeysWithDictionary:entity];
//找不到和key名字相同的属性时(苹果规定的优点复杂,具体可以看文档),会调用这个方法,重写可以进行特殊处理。
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
if ([key isEqualToString:@"MemberID"]) {
[self setValue:value forKey:@"memberID"];
}
if ([key isEqualToString:@"Area"]) {
[self setValue:value forKey:@"area"];
}
if ([key isEqualToString:@"CityName"]) {
[self setValue:value forKey:@"cityName"];
}
if ([key isEqualToString:@"MemberLevel"]) {
[self setValue:value forKey:@"memberLevel"];
}
}
比较
直接赋值:
1.需要判断字典key是否存在,如果不存在需要注意是不是写错了,或者和后台沟通有错误。
2.需要判断key对应的value是否为[NSNull null].
3.需要判断key对应的属性的类型,进行对应的处理
而通过kvc:
1.通过forUndefinedKey
处理entity中的key和属性的对应。如果entity中key和属性名字不同,如代码中的大小写不同,可以判断后手动修改。
2.如果entity中有些key不需要,重写forUndefinedKey
就就能安全处理。比如entity中有个key叫photo
,但是这个字段我们用不到,正常情况下程序会抛出异常,但是只要我们重写了- (void)setValue:(id)value forUndefinedKey:(NSString *)key
就没事。
2.[NSNull null]自动转化为nil。
3.自动根据属性类型进行转化
结论:
两种感觉差不多,根据需要选择。
网友评论