新浪微博开发十一(微博账号模型)

//
//  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值