ios 对象序列化与反序列化(存储plist文件)

.h

#import <Foundation/Foundation.h>

@interface WeiboUserInfo : NSObject<NSCoding>
{
    NSString *m_strDeviceJid;     //绑定的设备的jid
    NSString *m_strDevicePwd;     //绑定的设备的password
    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_strDevicePwd;
@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

.m文件

#import "WeiboUserInfo.h"

@implementation WeiboUserInfo
@synthesize m_strDeviceJid;     //绑定的设备的jid
@synthesize m_strDevicePwd;
@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_strDevicePwd 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_strDevicePwd forKey:@"m_strDevicePwd"];
    [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_strDevicePwd = [aDecoder decodeObjectForKey:@"m_strDevicePwd"];
        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

操作如#define kSinaUserInfo @"sinaBindUserInfo.plist"

- (void)savePlist
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *selfInfoPath = [documentsPath stringByAppendingPathComponent:kSinaUserInfo];
    
    if ([[NSFileManagerdefaultManager] fileExistsAtPath:selfInfoPath])
    {
        NSMutableArray *mutArrRecord = [[NSMutableArray alloc] initWithContentsOfFile:selfInfoPath];
        for (int i = 0; i < [mutArrRecord count]; i++)
        {
            NSData *dateRecord = [mutArrRecord objectAtIndex:i];
            WeiboUserInfo *weiboUserInfo = [NSKeyedUnarchiver unarchiveObjectWithData:dateRecord];
            if ([weiboUserInfo.m_strDeviceJid isEqualToString:[ABServices currentUserName]])
            {
          //移除,写入空 [mutArrRecord removeObjectAtIndex:i]; [mutArrRecord writeToFile:selfInfoPath atomically:YES]; break; } } [mutArrRecord release]; } }

  

  

posted @ 2013-11-18 16:13 暖流 阅读(...) 评论(...) 编辑 收藏