ios-新浪微博开发-18-(授权存储账号信息)用归档实现

88 篇文章 0 订阅
#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值