数据持久化基础知识——归档

使用对象模型进行归档,将对象写入文件,再从中读取它们。

1.首先的创建一个类,FourLines类.

FourLines.h

1 #import <Foundation/Foundation.h>
2 
3 //数据模型,存储当前存储在属性列表应用的字典中的数据。
4 @interface FourLines : NSObject <NSCopying, NSCoding>
5 @property (copy, nonatomic) NSArray * lines;
6 @end
View Code

FourLines.m

 1 #import "FourLines.h"
 2 
 3 static NSString * const kLinesKey = @"kLinesKey";
 4 
 5 @implementation FourLines
 6 
 7 #pragma mark - Coding
 8 //对4个属性进行解码
 9 - (id)initWithCoder:(NSCoder *)aDecoder
10 {
11     self = [super init];
12     if (self) {
13         self.lines = [aDecoder decodeObjectForKey:kLinesKey];
14     }
15     return self;
16 }
17 //对4个属性进行编码
18 - (void)encodeWithCoder:(NSCoder *)aCoder
19 {
20     [aCoder encodeObject:self.lines forKey:kLinesKey];
21 }
22 
23 #pragma mark - Coping
24 //新建一个对象,将4个字符串复制到对象中。
25 - (id)copyWithZone:(NSZone *)zone
26 {
27     FourLines * copy = [[[self class] allocWithZone:zone] init];
28     NSMutableArray * linesCopy = [NSMutableArray array];
29     for (id line in self.lines) {
30         [linesCopy addObject:[line copyWithZone:zone]];
31     }
32     copy.lines = linesCopy;
33     return copy;
34 }
35 @end
View Code

ViewController.m

 1 #import "ViewController.h"
 2 #import "FourLines.h"
 3 
 4 static NSString * const kRootKey = @"kRootKey";
 5 
 6 @interface ViewController ()
 7 @property (strong, nonatomic) IBOutletCollection(UITextField) NSArray * lineFields;
 8 @end
 9 
10 @implementation ViewController
11 
12 - (void)viewDidLoad {
13     [super viewDidLoad];
14     NSString * filePath = [self dataFilePath];
15     if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
16         
17 //        NSArray * array = [[NSArray alloc] initWithContentsOfFile:filePath];
18 //        for (int i = 0; i < 4; i++) {
19 //            UITextField * theField = self.lineFields[i];
20 //            theField.text = array[i];
21 //        }
22         
23         NSData * data = [[NSMutableData alloc] initWithContentsOfFile:filePath];
24         NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
25         FourLines * fourLines = [unarchiver decodeObjectForKey:kRootKey];
26         [unarchiver finishDecoding];
27         for (int i = 0; i < 4; i++) {
28             UITextField * theField = self.lineFields[i];
29             theField.text = fourLines.lines[i];
30         }
31     }
32     UIApplication * app = [UIApplication sharedApplication];
33     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
34 }
35 - (NSString *)dataFilePath
36 {
37     NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
38     NSString * documentsDirectory = [paths objectAtIndex:0];
39 //    return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
40     return [documentsDirectory stringByAppendingPathComponent:@"data.archive"];
41 }
42 - (void)applicationWillResignActive:(NSNotification *)notification
43 {
44     NSString * filePath = [self dataFilePath];
45 //    NSArray * array = [self.lineFields valueForKey:@"text"];
46 //    [array writeToFile:filePath atomically:YES];
47     
48     FourLines * fourLines = [[FourLines alloc] init];
49     fourLines.lines = [self.lineFields valueForKey:@"text"];
50     NSMutableData * data = [[NSMutableData alloc] init];
51     NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
52     [archiver encodeObject:fourLines forKey:kRootKey];
53     [archiver finishEncoding];
54     [data writeToFile:filePath atomically:YES];
55 }
56 - (void)didReceiveMemoryWarning {
57     [super didReceiveMemoryWarning];
58     // Dispose of any resources that can be recreated.
59 }
60 
61 @end
View Code
posted @ 2015-07-25 15:53  FMDN  阅读(170)  评论(0编辑  收藏  举报