Last active
March 13, 2016 07:12
-
-
Save fhefh2015/19bd75caa1357fb5bf68 to your computer and use it in GitHub Desktop.
iOS数据持久化
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// 数据持久化 | |
// | |
// Created by fhefh on 16/3/13. | |
// Copyright © 2016年 fhefh. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface Person:NSObject<NSCoding> | |
/** | |
* 姓名 | |
*/ | |
@property (nonatomic, copy) NSString *name; | |
/** | |
* 年龄 | |
*/ | |
@property (nonatomic, copy) NSString *age; | |
@end | |
@implementation Person | |
- (instancetype)initWithCoder:(NSCoder *)aDecoder { | |
if (self = [super init]) { | |
self.name = [aDecoder decodeObjectForKey:@"name"]; | |
self.age = [aDecoder decodeObjectForKey:@"age"]; | |
} | |
return self; | |
} | |
- (void)encodeWithCoder:(NSCoder *)aCoder { | |
[aCoder encodeObject:self.name forKey:@"name"]; | |
[aCoder encodeObject:self.age forKey:@"age"]; | |
} | |
@end | |
@interface ViewController () | |
@property (nonatomic, copy) NSString *path; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
//沙盒存储路径 | |
self.path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject; | |
} | |
- (IBAction)saveButton { | |
[self saveArchiver]; | |
} | |
- (IBAction)readButton { | |
[self readArchiver]; | |
NSLog(@"%@", self.path); | |
} | |
/** | |
plist文件 | |
plist文件是将某些特定的类,通过XML文件的方式保存在目录中。 | |
可以被序列化的类型只有如下几种: | |
NSArray | |
NSMutableArray | |
NSDictionary | |
NSMutableDictionary | |
NSData | |
NSMutableData | |
NSString | |
NSMutableString | |
NSNumber | |
NSDate | |
只有以上列出的类型才能使用plist文件存储。 | |
存储时使用writeToFile: atomically:方法。 其中atomically表示是否需要先写入一个辅助文件,再把辅助文件拷贝到目标文件地址。这是更安全的写入文件方法,一般都写YES。 | |
读取时使用arrayWithContentsOfFile:方法。 | |
*/ | |
//plist存储 | |
- (void)savePlist { | |
NSString *fileName = [self.path stringByAppendingPathComponent:@"data.plist"]; | |
NSArray *data = @[@"1", @"2", @"3"]; | |
[data writeToFile:fileName atomically:YES]; | |
} | |
//plist读取 | |
- (void)readPlist { | |
NSString *fileName = [self.path stringByAppendingPathComponent:@"data.plist"]; | |
NSArray *data = [NSArray arrayWithContentsOfFile:fileName]; | |
NSLog(@"%@", data); | |
} | |
/** | |
Preference | |
偏好设置是专门用来保存应用程序的配置信息的,一般不要在偏好设置中保存其他数据。 | |
如果没有调用synchronize方法,系统会根据I/O情况不定时刻地保存到文件中。所以如果需要立即写入文件的就必须调用synchronize方法。 | |
偏好设置会将所有数据保存到同一个文件中。即preference目录下的一个以此应用包名来命名的plist文件。 | |
*/ | |
//Preference存储 | |
- (void)readPreference { | |
NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; | |
[user setValue:@"19" forKey:@"age"]; | |
[user setValue:@"jack" forKey:@"name"]; | |
[user synchronize]; | |
} | |
//Preferencex读取 | |
- (void)savePreference { | |
NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; | |
NSString *name = [user objectForKey:@"name"]; | |
NSString *age = [user objectForKey:@"age"]; | |
NSLog(@"name is %@, age is %@", name, age); | |
} | |
//NSKeyedArchiver | |
/** | |
归档在iOS中是另一种形式的序列化,只要遵循了NSCoding协议的对象都可以通过它实现序列化。由于决大多数支持存储数据的Foundation和Cocoa Touch类都遵循了NSCoding协议,因此,对于大多数类来说,归档相对而言还是比较容易实现的。 | |
1.遵循NSCoding协议 | |
NSCoding协议声明了两个方法,这两个方法都是必须实现的。一个用来说明如何将对象编码到归档中,另一个说明如何进行解档来获取一个新对象。 | |
特别注意如果需要归档的类是某个自定义类的子类时,就需要在归档和解档之前先实现父类的归档和解档方法。即 [super encodeWithCoder:aCoder] 和 [super initWithCoder:aDecoder] 方法; | |
2.使用需要把对象归档是调用NSKeyedArchiver的工厂方法 archiveRootObject: toFile: 方法。 | |
3.注意 | |
必须遵循并实现NSCoding协议 | |
保存文件的扩展名可以任意指定 | |
继承时必须先调用父类的归档解档方法 | |
*/ | |
//NSKeyedArchiver存储 | |
- (void)saveArchiver { | |
NSString *fileName = [self.path stringByAppendingPathComponent:@"save.data"]; | |
Person *p = [[Person alloc] init]; | |
p.name = @"jack"; | |
p.age = @"19"; | |
[NSKeyedArchiver archiveRootObject:p toFile:fileName]; | |
} | |
//NSKeyedArchiver读取 | |
- (void)readArchiver { | |
NSString *fileName = [self.path stringByAppendingPathComponent:@"save.data"]; | |
Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName]; | |
NSLog(@"%@", p); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment