iOS对象序列化和反序列化

来源:互联网 发布:电视用网络怎么看电视 编辑:程序博客网 时间:2024/05/12 02:28
#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 (nonatomiccopyNSString *m_strDeviceJid;
@property (nonatomiccopyNSString *m_strDevicePwd;
@property (nonatomiccopyNSString *m_strSinaJid;
@property (nonatomiccopyNSString *m_strSinaPasswd;
@property (nonatomiccopyNSString *m_strSinaBrief;
@property (nonatomiccopyNSString *m_strNickName;
@property (nonatomiccopyNSString *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文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- (void)savePlist
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);
    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]])
            {<br>          //移除,写入空
                [mutArrRecord removeObjectAtIndex:i];
                [mutArrRecord writeToFile:selfInfoPath atomically:YES];
                break;
            }
        }
        [mutArrRecord release];
    }
}


转自:http://www.cnblogs.com/zcw-ios/articles/3429680.html
0 0