zoukankan      html  css  js  c++  java
  • iOS 数据持久性存储-对象归档

    对象归档是将对象归档以文件的形式保存到磁盘中(也称为序列化,持久化),使用的时候读取该文件的保存路径读取文件的内容(也称为解档,反序列化)

    主要涉及两个类:NSKeyedArichiver、NSKeyedUnarchiver

    两个协议NSCoding的2个方法:

      - (void)encodeWithCoder:

      - (id)initWithCoder:

    可以接着(一)的例子,新建文件,subclass of NSObject, 实现NSCoding协议

    新建文件.h

    #import <Foundation/Foundation.h>
    
    @interface BSLFourLines : NSObject<NSCoding>
    
    @property (copy, nonatomic) NSString *field1;
    @property (copy, nonatomic) NSString *field2;
    @property (copy, nonatomic) NSString *field3;
    @property (copy, nonatomic) NSString *field4;
    
    @end
    View Code

    新建文件.m

    #import "BSLFourLines.h"
    #define kField1Key @"Field1"
    #define kField2Key @"Field2"
    #define kField3Key @"Field3"
    #define kField4Key @"Field4"
    
    @implementation BSLFourLines
    @synthesize field1,field2,field3,field4;
    
    #pragma mark NSCoding
    //以keyValue形式对基本数据类型Encoding
    - (void)encodeWithCoder:(NSCoder *)aCoder{
        [aCoder encodeObject:field1 forKey:kField1Key];
        [aCoder encodeObject:field2 forKey:kField2Key];
        [aCoder encodeObject:field3 forKey:kField3Key];
        [aCoder encodeObject:field4 forKey:kField4Key];
    }
    
    //以keyValue形式对基本数据类型Decoding,返回数据模型本身
    - (id)initWithCoder:(NSCoder *)aDecoder{
        if (self = [super init]) {
            field1 = [aDecoder decodeObjectForKey:kField1Key];
            field2 = [aDecoder decodeObjectForKey:kField2Key];
            field3 = [aDecoder decodeObjectForKey:kField3Key];
            field4 = [aDecoder decodeObjectForKey:kField4Key];
        }
        return self;
    }
    View Code

    现在转到XXViewController.m在#import下方添加

    #import "BSLFourLines.h"

    #define kFilename2 @"archive"

    #define kDataKey @"Data"

    改动后的方法内容

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSString *filePath = [self dataFilePath];
        //检查数据文件是否存在,如果存在就用此文件内容实例化数组
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
            
            NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
            NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            BSLFourLines *fourLines = [unarchiver decodeObjectForKey:kDataKey];
            [unarchiver finishDecoding];
       
            field1.text = fourLines.field1;
            field2.text = fourLines.field2;
            field3.text = fourLines.field3;
            field4.text = fourLines.field4;
        }
    //通知
        UIApplication *app = [UIApplication sharedApplication];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
    }
    View Code
    - (void)applicationWillResignActive:(NSNotification *)notification{
        
        BSLFourLines *fourLines = [[BSLFourLines 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:kDataKey];
        [archiver finishEncoding];
        [data writeToFile:[self dataFilePath] atomically:YES];
    }
    View Code

    如果需要对自定义的类进行copy的话,需要在自定义类实现<NSCoding>协议

    自定义类的实现文件里添加下列方法

    - (id)copyWithZone:(NSZone *)zone{
        BSLFourLines *copy = [[[self class] allocWithZone:zone] init];
        copy.field1 = [self.field1 copyWithZone:zone];
        copy.field2 = [self.field2 copyWithZone:zone];
        copy.field3 = [self.field3 copyWithZone:zone];
        copy.field4 = [self.field4 copyWithZone:zone];
        return copy;
    }
    View Code
  • 相关阅读:
    1007 Maximum Subsequence Sum(25 分)
    1006 Sign In and Sign Out(25 分)
    1005 Spell It Right
    1004 Counting Leaves
    Struts10分钟入门
    MyBais入门
    Hibernate注解
    save,flush,evict
    HQL连接查询
    Hibernate-延迟加载和立即加载
  • 原文地址:https://www.cnblogs.com/sparks/p/3829084.html
Copyright © 2011-2022 走看看