ios序列化对象存储本地


#import

@interface WeiboUserInfo : NSObject
{
NSString *m_strDeviceJid; //绑定的设备的jid
NSString *m_strSinaJid; //sina帐号的jid
NSString *m_strSinaPasswd; //sina帐号密码
NSString *m_strNickName; //sina帐号的昵称
NSString *m_strSinaBrief; //sina个性签名

NSString *m_strAccessToken; //sina accessToken
NSDate *m_strExpirationDate; //accessToken 到期时间

NSData *m_imgPortraitSmall; //头像数据(小图片)
NSData *m_imgPortraitMid; //头像数据(中图片)
NSData *m_imgPortraitLarge; //头像数据(大图片)
}

@property (nonatomic, copy) NSString *m_strDeviceJid;
@property (nonatomic, copy) NSString *m_strSinaJid;
@property (nonatomic, copy) NSString *m_strSinaPasswd;
@property (nonatomic, copy) NSString *m_strSinaBrief;
@property (nonatomic, copy) NSString *m_strNickName;
@property (nonatomic, copy) NSString *m_strAccessToken;
@property (nonatomic, retain) NSDate *m_strExpirationDate;

@property (nonatomic, retain) NSData *m_imgPortraitSmall;
@property (nonatomic, retain) NSData *m_imgPortraitMid;
@property (nonatomic, retain) NSData *m_imgPortraitLarge;

@end



#import "WeiboUserInfo.h"

@implementation WeiboUserInfo
@synthesize m_strDeviceJid; //绑定的设备的jid
@synthesize m_strSinaJid; //sina帐号的jid
@synthesize m_strSinaPasswd; //sina帐号密码
@synthesize m_strNickName; //sina帐号的昵称
@synthesize m_strSinaBrief;

@synthesize m_strAccessToken; //sina accessToken
@synthesize m_strExpirationDate; //accessToken 到期时间

@synthesize m_imgPortraitSmall; //头像数据(小图片)
@synthesize m_imgPortraitMid; //头像数据(中图片)
@synthesize m_imgPortraitLarge; //头像数据(大图片)

- (void)dealloc
{
[m_strDeviceJid release];
[m_strSinaJid release];
[m_strSinaPasswd release];
[m_strNickName release];
[m_strSinaBrief release];

[m_strAccessToken release];
[m_strExpirationDate release];

[m_imgPortraitSmall release];
[m_imgPortraitMid release];
[m_imgPortraitLarge release];

[super dealloc];
}

//将对象编码(即:序列化)
-(void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:m_strDeviceJid forKey:@"m_strDeviceJid"];
[aCoder encodeObject:m_strSinaJid forKey:@"m_strSinaJid"];
[aCoder encodeObject:m_strSinaPasswd forKey:@"m_strSinaPasswd"];
[aCoder encodeObject:m_strNickName forKey:@"m_strNickName"];
[aCoder encodeObject:m_strSinaBrief forKey:@"m_strSinaBrief"];
[aCoder encodeObject:m_strAccessToken forKey:@"m_strAccessToken"];
[aCoder encodeObject:m_strExpirationDate forKey:@"m_strExpirationDate"];
[aCoder encodeObject:m_imgPortraitSmall forKey:@"m_imgPortraitSmall"];
[aCoder encodeObject:m_imgPortraitMid forKey:@"m_imgPortraitMid"];
[aCoder encodeObject:m_imgPortraitLarge forKey:@"m_imgPortraitLarge"];
}

//将对象解码(反序列化)
-(id) initWithCoder:(NSCoder *)aDecoder
{
if (self=[super init])
{
self.m_strDeviceJid =[aDecoder decodeObjectForKey:@"m_strDeviceJid"];
self.m_strSinaJid =[aDecoder decodeObjectForKey:@"m_strSinaJid"];
self.m_strSinaPasswd =[aDecoder decodeObjectForKey:@"m_strSinaPasswd"];
self.m_strNickName =[aDecoder decodeObjectForKey:@"m_strNickName"];
self.m_strSinaBrief =[aDecoder decodeObjectForKey:@"m_strSinaBrief"];
self.m_strAccessToken =[aDecoder decodeObjectForKey:@"m_strAccessToken"];
self.m_strExpirationDate =[aDecoder decodeObjectForKey:@"m_strExpirationDate"];
self.m_imgPortraitSmall =[aDecoder decodeObjectForKey:@"m_imgPortraitSmall"];
self.m_imgPortraitMid =[aDecoder decodeObjectForKey:@"m_imgPortraitMid"];
self.m_imgPortraitLarge =[aDecoder decodeObjectForKey:@"m_imgPortraitLarge"];
}
return (self);

}

@end


声明好之后在调用时就可以存本地plist了,以下调用函数

WeiboUserInfo *weiboUserInfo = [NSKeyedUnarchiver unarchiveObjectWithData:[m_arrUserInfo objectAtIndex:indexPath.row]];

NSData *date = [NSKeyedArchiver archivedDataWithRootObject:m_weiboUserInfo];

你可能感兴趣的:(iPhone开发)