zoukankan      html  css  js  c++  java
  • 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];       

        }

  • 相关阅读:
    利用Mathematica计算伴随矩阵
    一个游戏
    华南理工大学2016年数学分析高等代数考研试题参考解答
    中山大学2016年数学分析高等代数考研试题参考解答及其讲解
    张祖锦第7卷第483期一个对数-平方根不等式
    为新生儿办理户口
    丘成桐大学生数学竞赛2014年分析与方程个人赛试题第一题另解
    家里蹲大学数学杂志第7卷第481期一道实分析题目参考解答
    顶级俄国数学家是怎样炼成的?[2016-06-25 张羿 赛先生]
    Calculations are rather interesting
  • 原文地址:https://www.cnblogs.com/AbelChen1991/p/3730808.html
Copyright © 2011-2022 走看看