首页 > 其他 > 详细

NSUserDefaults写入和读取自定义的对象

时间:2014-06-22 07:58:19      阅读:408      评论:0      收藏:0      [点我收藏+]

需要写入的对象必须实现NSCoding protocol

Person Class

Person.h

#import <Foundation/Foundation.h>

#import "Face.h"

@interface Person : NSObject <NSCoding>

@property (nonatomic, strong) NSString      *personId;
@property (nonatomic, strong) NSString      *name;
@property (nonatomic, assign) NSInteger      age;
@property (nonatomic, strong) NSArray       *photos;
@property (nonatomic, strong) NSDictionary  *phoneNumber;

@property (nonatomic, strong) Face          *face;

@end


Person.m

#import "Person.h"

@implementation Person
@synthesize personId    = _personId;
@synthesize name        = _name;
@synthesize age         = _age;
@synthesize photos      = _photos;
@synthesize phoneNumber = _phoneNumber;
@synthesize face        = _face;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init])
    {
        self.personId       = [aDecoder decodeObjectForKey:@"id"];
        self.name           = [aDecoder decodeObjectForKey:@"name"];
        self.age            = [[aDecoder decodeObjectForKey:@"age"] integerValue];
        self.photos         = [aDecoder decodeObjectForKey:@"photos"];
        self.phoneNumber    = [aDecoder decodeObjectForKey:@"phoneNumber"];
        self.face           = [aDecoder decodeObjectForKey:@"face"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.personId forKey:@"id"];
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:[NSNumber numberWithInteger:self.age] forKey:@"age"];
    [aCoder encodeObject:self.photos forKey:@"photos"];
    [aCoder encodeObject:self.phoneNumber forKey:@"phoneNumber"];
    [aCoder encodeObject:self.face forKey:@"face"];
}

Face Class

Face.h

#import <Foundation/Foundation.h>

@interface Face : NSObject <NSCoding>

@property (nonatomic, strong) NSString *head;
//@property (nonatomic, strong) NSString *eyes;
//@property (nonatomic, strong) NSString *nose;
//@property (nonatomic, strong) NSString *mouth;
//@property (nonatomic, strong) NSString *ears;

@end

Face.m

#import "Face.h"

@implementation Face

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init])
    {
        self.head   = [aDecoder decodeObjectForKey:@"head"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.head forKey:@"head"];
}

@end

写入和读取操作

    Person *person = [[Person alloc] init];
    person.personId = @"123456789";
    person.name = @"Hunk";
    person.age = 10;
    person.photos = @[@"a.png", @"b.png", @"c.png"];
    person.phoneNumber = @{@"mobile_phone" : @"987654321", @"work" : @"01012345678"};
    Face *face = [[Face alloc] init];
    face.head  = @"Round shape";
    person.face = face;
    
    NSData *personData0 = [NSKeyedArchiver archivedDataWithRootObject:person];
    
    [[NSUserDefaults standardUserDefaults] setObject:personData0 forKey:@"person"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    NSData *personData1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"person"];
    Person *person1 = [NSKeyedUnarchiver unarchiveObjectWithData:personData1];


NSUserDefaults写入和读取自定义的对象,布布扣,bubuko.com

NSUserDefaults写入和读取自定义的对象

原文:http://blog.csdn.net/lixuwen521/article/details/32965297

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!