• 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];       

        }

  • 相关阅读:
    [转]Convolution Neural Network (CNN) 原理与实现
    [转]深度学习CNN研究反向
    [转]一张图看懂:Google AlphaGo的原理、弱点
    [转]前馈型神经网络与反馈型神经网络的区别
    [转]认知机和神经认知机
    [转]技术向:一文读懂卷积神经网络CNN
    PHP 日期格式化 参数参考
    PHP MAIL DEMO(程序代码直接发送邮件)
    PHP上传文件DEMO
    PDO事务管理DEMO
  • 原文地址:https://www.cnblogs.com/AbelChen1991/p/3730808.html
Copyright © 2020-2023  润新知