今天给大家介绍一个OC中的数据持久化,今天介绍其中的三种,plist存储,偏好设置存储及归档存储!下面分别给大家上代码,并在代码中进行详细注释!
一 plist存储(只能存储OC中这些如字典,数组等常见的数据类型,如果想存个自定义类型的数据是不支持的)
//存数据- (void)saveArray{ // 1.获得沙盒根路径 NSString *home = NSHomeDirectory(); // 2.document路径 NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; // 3.新建数据 NSArray *data = @[@"jack", @10, @"ffdsf"]; NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"]; [data writeToFile:filepath atomically:YES];}//读取数据- (IBAction)read { // 1.获得沙盒根路径 NSString *home = NSHomeDirectory(); // 2.document路径 NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; // 3.文件路径 NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"]; // 4.读取数据 NSArray *data = [NSArray arrayWithContentsOfFile:filepath]; NSLog(@"%@", data);}
二 偏好设置存储(故名思意,偏好设置存储就是存储我们APP的一些偏好设置,例如xcode中的preference)
- (IBAction)save { // 1.利用NSUserDefaults,就能直接访问软件的偏好设置(Library/Preferences) NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // 2.存储数据 [defaults setObject:@"mm" forKey:@"account"]; [defaults setObject:@"123" forKey:@"pwd"]; [defaults setInteger:10 forKey:@"age"]; [defaults setBool:YES forKey:@"auto_login"]; // 3.立刻同步 [defaults synchronize];}- (IBAction)read { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *account = [defaults objectForKey:@"account"]; BOOL autoLogin = [defaults boolForKey:@"auto_login"]; NSLog(@"%@ -- %d", account, autoLogin);}
三 归档
ViewController类
#import "ViewController.h"#import "Student.h"@interface ViewController ()- (IBAction)save;- (IBAction)read;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (IBAction)save { // 1.新的模型对象 Student *stu = [[Student alloc] init]; stu.no = @"42343254"; stu.age = 20; stu.height = 1.55; // 2.归档模型对象 // 2.1.获得Documents的全路径 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // 2.2.获得文件的全路径 NSString *path = [doc stringByAppendingPathComponent:@"stu.data"]; // 2.3.将对象归档 [NSKeyedArchiver archiveRootObject:stu toFile:path];}- (IBAction)read { // 1.获得Documents的全路径 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // 2.获得文件的全路径 NSString *path = [doc stringByAppendingPathComponent:@"stu.data"]; // 3.从文件中读取MJStudent对象 Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; NSLog(@"%@ %d %f", stu.no, stu.age, stu.height);}@end
Student类
student.h(这里一定要遵守NSCoding协议,因为归档与反归档要利用NSCoding协议来对要存储的数据来进行编码 ,如果不遵守此协议便会报错!大家切记):
#import@interface Student : NSObject @property (nonatomic, copy) NSString *no;@property (nonatomic, assign) double height;@property (nonatomic, assign) int age;@end
student.m(这里要实现这两个方法,- (void)encodeWithCoder:(NSCoder *)encoder这个方法是对数据进行存储,并指定对哪几个数据进行存储;- (id)initWithCoder:(NSCoder *)decoder 显而易见这个方法肯定是要告诉我们读取哪些数据了!
#import "Student.h"@interface Student() @end@implementation Student/** * 将某个对象写入文件时会调用 * 在这个方法中说清楚哪些属性需要存储 */- (void)encodeWithCoder:(NSCoder *)encoder{ [encoder encodeObject:self.no forKey:@"no"]; [encoder encodeInt:self.age forKey:@"age"]; [encoder encodeDouble:self.height forKey:@"height"];}/** * 从文件中解析对象时会调用 * 在这个方法中说清楚哪些属性需要存储 */- (id)initWithCoder:(NSCoder *)decoder{ if (self = [super init]) { // 读取文件的内容 self.no = [decoder decodeObjectForKey:@"no"]; self.age = [decoder decodeIntForKey:@"age"]; self.height = [decoder decodeDoubleForKey:@"height"]; } return self;}@end
到此为止如果有不明白的地方请给我留言,会详细解答.