// MyAccountModel.h
// 新浪微博
//
// Created by jose on 15-3-24.
// Copyright (c) 2015年 jose. All rights reserved.
//
#import <Foundation/Foundation.h>
//<NSCoding>协议
@interface MyAccountModel : NSObject<NSCoding>
/** access_token string 获取授权后的access_token */
@property(nonatomic,copy)NSString *access_token;
/** expires_in string access_token的声明周期,单位是秒数 */
@property(nonatomic,copy)NSString *expires_in;
/** uid string 当前授权用户的UID **/
@property(nonatomic,copy)NSString *uid;
/** expires_time nsdate access_token的过期时间 */
@property(nonatomic,copy)NSDate *expires_time;
//面向模型开发
+(instancetype)AccountModelWithDict:(NSDictionary *)dict;
@end
*************************************************************************************************************
************************************************************************************************************
************************************************************************************************************
实现文件:
//
// MyAccountModel.m
// 新浪微博
//
// Created by jose on 15-3-24.
// Copyright (c) 2015年 jose. All rights reserved.
//
#import "MyAccountModel.h"
@implementation MyAccountModel
+(instancetype)AccountModelWithDict:(NSDictionary *)dict{
//注意,json中返回的数据和model中的成员属性不是一一对应,因此不能直接使用KVC
MyAccountModel *model=[[self alloc]init];
model.access_token=dict[@"access_token"];
model.expires_in=dict[@"expires_in"];
model.uid=dict[@"uid"];
//获取当前时间
NSDate *now=[NSDate date];
//账号过期时间=创建时间+有效期
model.expires_time=[now dateByAddingTimeInterval:model.expires_in.doubleValue];
return model;
}
#pragma mark 使用归档需要遵守<NSCoding>协议
//从文件中解析出一个对象的时候调用
-(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"];
self.expires_time=[aDecoder decodeObjectForKey:@"expires_time"];
}
return self;
}
//将一个对象写入文件的时候调用
-(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"];
[aCoder encodeObject:self.expires_time forKey:@"expires_time"];
}
@end