#import <Foundation/Foundation.h>
#warning 这里必须遵守协议 不然不知道对象中的哪些属性写进沙盒
@interface QHAccount : NSObject <NSCoding>
// 返回值字段 字段类型 字段说明
/** string 用于调用access_token,接口获取授权后的access token。*/
@property(nonatomic,copy)NSString * access_token;
/**string access_token的生命周期,单位是秒数。*/
@property(nonatomic,copy)NSString * expires_in;
/**string 当前授权用户的UID。*/
@property(nonatomic,copy)NSString *uid;
+ (instancetype)accountWithDict:(NSDictionary *)dict;
@end
#import "QHAccount.h"
@implementation QHAccount
+ (instancetype)accountWithDict:(NSDictionary *)dict
{
QHAccount *account = [[self alloc]init];
account.access_token = dict[@"access_token"];
account.uid = dict[@"uid"];
account.expires_in = dict[@"expires_in"];
return account;
}
/**
* 当一个对象要归档进沙盒中时,就会调用这个方法
* 目的:在这个方法中说明这个对象的哪些属性要存进沙盒
*/
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.access_token forKey:@"access_token"];
[aCoder encodeObject:self.expires_in forKey:@"expires_in"];
[aCoder encodeObject:self.uid forKey:@"uid"];
}
/**
* 当从沙盒中解档一个对象时(从沙盒中加载一个对象时)就会调用这个方法
* 目的:在这个方法中说明沙盒中的属性该怎么解析 (需要取出哪些属性)
*/
- (id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super init])
{
self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
self.expires_in = [aDecoder decodeObjectForKey:@"expires_in"];
self.uid = [aDecoder decodeObjectForKey:@"uid"];
}
return self;
}
@end