IPhone之模型对象归档

归档指的是另一种型式的序列化,但它是任保对象都可以实现的更常规的类型。

  其作用为:进行数据的持久化保存。

  对象必须实现NSCodeing协议和NSCopying协议。

@interface FourLines : NSObject <NSCoding, NSCopying> {

    NSString *field1;

    NSString *field2;

    NSString *field3;

    NSString *field4;   

}

@property (nonatomic, retain) NSString *field1;

@property (nonatomic, retain) NSString *field2;

@property (nonatomic, retain) NSString *field3;

@property (nonatomic, retain) NSString *field4;

@end
#pragma mark NSCoding

- (void)encodeWithCoder:(NSCoder *)encoder {

    [encoder encodeObject:field1 forKey:kField1Key];

    [encoder encodeObject:field2 forKey:kField2Key];

    [encoder encodeObject:field3 forKey:kField3Key];

    [encoder encodeObject:field4 forKey:kField4Key];

}

- (id)initWithCoder:(NSCoder *)decoder {

    if (self = [super init]) {

        self.field1 = [decoder decodeObjectForKey:kField1Key];

        self.field2 = [decoder decodeObjectForKey:kField2Key];

        self.field3 = [decoder decodeObjectForKey:kField3Key];

        self.field4 = [decoder decodeObjectForKey:kField4Key];

    }

    return self;

}
#pragma mark NSCopying

- (id)copyWithZone:(NSZone *)zone {

    FourLines *copy = [[[self class] allocWithZone: zone] init];

    copy.field1 = [[self.field1 copyWithZone:zone] autorelease];

    copy.field2 = [[self.field2 copyWithZone:zone] autorelease];

    copy.field3 = [[self.field3 copyWithZone:zone] autorelease];

    copy.field4 = [[self.field4 copyWithZone:zone] autorelease];

   

    return copy;

}

 

获取归档文件

- (NSString *)dataFilePath {

    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    return [documentsDirectory stringByAppendingPathComponent:@"archive"];

}

 

对数据进行归档

FourLines *fourLines = [[FourLines alloc] init];

    fourLines.field1 = field1.text;

    fourLines.field2 = field2.text;

    fourLines.field3 = field3.text;

    fourLines.field4 = field4.text;

   

//对数据进行归档

    NSMutableData *data = [[NSMutableData alloc] init];

    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

    [archiver encodeObject:fourLines forKey:@"Data"];

    [archiver finishEncoding];

    [data writeToFile:[self dataFilePath] atomically:YES];

    [fourLines release];

    [archiver release];

    [data release];   

 

获取归档数据

    NSString *filePath = [self dataFilePath];

    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

        NSData *data = [[NSMutableData alloc]

                        initWithContentsOfFile:[self dataFilePath]];

        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]

                                         initForReadingWithData:data];

        FourLines *fourLines = [unarchiver decodeObjectForKey:@"Data"];

        [unarchiver finishDecoding];

       

        field1.text = fourLines.field1;

        field2.text = fourLines.field2;

        field3.text = fourLines.field3;

        field4.text = fourLines.field4;

       

        [unarchiver release];

        [data release];       

    }

原文地址:https://www.cnblogs.com/AbelChen1991/p/3730808.html