初始化方法
#import "User.h"
#import <objc/message.h>
@implementation User
- (id) initWithDicionary:(NSDictionary*)dic {
self = [super init];
if (self) {
//更新用户信息:
[self updateUserInfo:dic];
}
return self;
}
更新用户信息
- (void)updateUserInfo:(NSDictionary *)dic {
id userId = [[dic objectForKey:@"data"] objectForKey:@"user_id"];
if ([userId isKindOfClass:[NSNull class]]) self.user_id = @"0000";
self.user_id = userId;
NSString *nickName = [[dic objectForKey:@"data"] objectForKey:@"nickname"];
if ([nickName isKindOfClass:[NSNull class]]) self.nickname = @"";
self.nickname = nickName;
id mobile = [[dic objectForKey:@"data"] objectForKey:@"mobile"];
if ([mobile isKindOfClass:[NSNull class]]) self.moblie = @"0000";
self.moblie = mobile;
id lng = [[dic objectForKey:@"data"] objectForKey:@"lng"];
if ([lng isKindOfClass:[NSNull class]]) self.lng = @"00.00";
self.lng = lng;
id lat = [[dic objectForKey:@"data"] objectForKey:@"lat"];
if ([lat isKindOfClass:[NSNull class]]) self.lat = @"00.00";
self.lat = lat;
NSString *avater = [[dic objectForKey:@"data"] objectForKey:@"avatar"];
if ([avater isKindOfClass:[NSNull class]]) self.avatar = @"";
self.avatar = avater;
NSString *token = [dic objectForKey:@"token"];
if ([token isKindOfClass:[NSNull class]]) self.token = @"";
self.token = token;
NSString *describe = [[dic objectForKey:@"data"] objectForKey:@"describe"];
if ([describe isKindOfClass:[NSNull class]]) self.describe = @"";
self.describe = describe;
NSString *signature = [[dic objectForKey:@"data"] objectForKey:@"signature"];
if ([signature isKindOfClass:[NSNull class]]) self.signature = @"";
self.signature = signature;
NSString *gender = [[dic objectForKey:@"data"] objectForKey:@"gender"];
if ([gender isKindOfClass:[NSNull class]]) self.gender = @"";
self.gender = gender;
NSString *school = [[dic objectForKey:@"data"] objectForKey:@"school"];
if ([school isKindOfClass:[NSNull class]]) self.school = @"";
self.school = school;
NSString *department = [[dic objectForKey:@"data"] objectForKey:@"department"];
if ([department isKindOfClass:[NSNull class]]) self.department = @"";
self.department = department;
NSString *start_time = [[dic objectForKey:@"data"] objectForKey:@"start_time"];
if ([start_time isKindOfClass:[NSNull class]]) self.start_time = @"";
self.start_time = start_time;
NSString *education_id = [[dic objectForKey:@"data"] objectForKey:@"education_id"];
if ([education_id isKindOfClass:[NSNull class]]) self.education_id = @"";
self.education_id = education_id;
}
runtime遍历属性
/*
对象归档解档改为运行时进行:
*/
#pragma mark - 对象归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
unsigned int count = 0 ;
Ivar *ivars = class_copyIvarList([User class], &count) ;
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i] ;
const char *propertyName = ivar_getName(ivar) ;
NSString *key = [[NSString alloc] initWithUTF8String:propertyName] ;
[aCoder encodeObject:[self valueForKey:key] forKey:key] ;
}
}
#pragma mark - 对象解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init] ;
if (self) {
unsigned int count = 0 ;
Ivar *ivars = class_copyIvarList([User class], &count) ;
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i] ;
const char *propertyName = ivar_getName(ivar) ;
NSString *key = [[NSString alloc] initWithUTF8String:propertyName] ;
id value = [aDecoder decodeObjectForKey:key] ;
[self setValue:value forKey:key] ;
}
}
return self ;
}
原始方法一个一个去写
// 归档(NSCoding 协议方法):
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.user_id forKey:@"user_id"];
[aCoder encodeObject:self.nickname forKey:@"nickname"];
[aCoder encodeObject:self.moblie forKey:@"moblie"];
[aCoder encodeObject:self.lng forKey:@"lng"];
[aCoder encodeObject:self.lat forKey:@"lat"];
[aCoder encodeObject:self.token forKey:@"token"];
[aCoder encodeObject:self.avatar forKey:@"avatar"];
[aCoder encodeObject:self.describe forKey:@"describe"];
[aCoder encodeObject:self.signature forKey:@"signature"];
[aCoder encodeObject:self.gender forKey:@"gender"];
[aCoder encodeObject:self.school forKey:@"school"];
[aCoder encodeObject:self.department forKey:@"department"];
[aCoder encodeObject:self.start_time forKey:@"start_time"];
[aCoder encodeObject:self.education_id forKey:@"education_id"];
}
// 反归档(NSCoding 协议方法):
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
self.user_id = [aDecoder decodeObjectForKey:@"user_id"];
self.nickname = [aDecoder decodeObjectForKey:@"nickname"];
self.moblie = [aDecoder decodeObjectForKey:@"moblie"];
self.lng = [aDecoder decodeObjectForKey:@"lng"];
self.lat = [aDecoder decodeObjectForKey:@"lat"];
self.token = [aDecoder decodeObjectForKey:@"token"];
self.avatar = [aDecoder decodeObjectForKey:@"avatar"];
self.describe = [aDecoder decodeObjectForKey:@"describe"];
self.signature = [aDecoder decodeObjectForKey:@"signature"];
self.gender = [aDecoder decodeObjectForKey:@"gender"];
self.school = [aDecoder decodeObjectForKey:@"school"];
self.department = [aDecoder decodeObjectForKey:@"department"];
self.start_time = [aDecoder decodeObjectForKey:@"start_time"];
self.education_id = [aDecoder decodeObjectForKey:@"education_id"];
}
return self;
}
重写description方法打印
- (NSString *)description {
return [NSString stringWithFormat:@"%@, %@, %@, %@, %@, %@, %@", _user_id, _nickname, _avatar, _moblie, _token, _lat, _lng];
}
@end