在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦;
偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置信息)
归档:因为前两者都有一个致命的缺陷,只能存储常用的类型。归档可以实现把自定义的对象存放在文件中。
代码演示
先看下项目的结构 Student类继承Person类
在ViewController.m中
//
// ViewController.m
// Archive
//
// Created by City--Online on 15/4/22.
// Copyright (c) 2015年 CYW. All rights reserved.
//
#import "ViewController.h"
#import "Student.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentpath=[documents firstObject];
// NSLog(@"%@",documentpath);
NSString *filePath=[documentpath stringByAppendingPathComponent:@"CYW"];
//使用archiveRootObject简单归档 单个对象
//缺点:只能把一个对象归档到一个文件
#if 0
NSArray *arr=[[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil];
//归档 //可对字典、数组、字符串、数字等进行归档 返回bool值表示是否归档成功
[NSKeyedArchiver archiveRootObject:arr toFile:filePath];
#endif
#if 0
//解挡
NSArray *arr=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",arr);
#endif
//多个对象归档
#if 0
//准备数据
CGPoint point=CGPointMake(100, 100);
NSString *mystring=@"cuiyanwei";
BOOL YesOrNo=YES;
//归档
NSMutableData *mutableData=[[NSMutableData alloc]init];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData];
[archiver encodeCGPoint:point forKey:@"mypoint"];
[archiver encodeBool:YesOrNo forKey:@"mybool"];
[archiver encodeObject:mystring forKey:@"mystring"];
[archiver finishEncoding];
[mutableData writeToFile:filePath atomically:YES];
#endif
#if 0
//多对象解档
NSMutableData *mutableData=[[NSMutableData alloc]initWithContentsOfFile:filePath];
NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:mutableData];
CGPoint point=[unarchiver decodeCGPointForKey:@"mypoint"];
BOOL mybool=[unarchiver decodeBoolForKey:@"mybool"];
NSString *mystring=[unarchiver decodeObjectForKey:@"mystring"];
[unarchiver finishDecoding];
NSLog(@"X=%lf,Y=%lf\n mybool=%d\n mystring=%@",point.x,point.y,mybool,mystring);
UIImage
#endif
#if 0
//自定义对象归档
Student *stu1=[[Student alloc]init];
stu1.name=@"cuiyanwei";
stu1.age=24;
stu1.photo=[UIImage imageNamed:@"email.png"];
stu1.stuId=@"001";
Student *stu2=[[Student alloc]init];
stu2.name=@"xiaocui";
stu2.age=23;
stu2.photo=[UIImage imageNamed:@"email.png"];
stu2.stuId=@"002";
//归档
NSArray *array=[[NSArray alloc]initWithObjects:stu1,stu2, nil];
NSMutableData *mutableData=[[NSMutableData alloc]init];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData];
[archiver encodeObject:array forKey:@"students"];
[archiver finishEncoding];
[mutableData writeToFile:filePath atomically:YES];
#endif
#if 1
//解档
NSMutableData *mutableData=[[NSMutableData alloc]initWithContentsOfFile:filePath];
NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:mutableData];
NSArray *array=[unarchiver decodeObjectForKey:@"students"];
for (Student *stu in array) {
NSLog(@"name=%@ age=%ld photo=%@ stuId=%@",stu.name,stu.age,stu.photo,stu.stuId);
}
#endif
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在Person.h中定义了几个属性
//
// Person.h
// Archive
//
// Created by City--Online on 15/4/22.
// Copyright (c) 2015年 CYW. All rights reserved.
//
#import <Foundation/Foundation.h>
//头像要添加框架在类中
#import <UIKit/UIKit.h>
//自定义保存到文件需要实现NSCoding协议
@interface Person : NSObject<NSCoding>
@property(nonatomic,strong)NSString *name;
@property(nonatomic,assign)NSInteger age;
@property(nonatomic,strong)UIImage *photo;
@end
在Person.m中实现NSCoding协议
//
// Person.m
// Archive
//
// Created by City--Online on 15/4/22.
// Copyright (c) 2015年 CYW. All rights reserved.
//
#import "Person.h"
@implementation Person
- (void)encodeWithCoder:(NSCoder *)aCoder
{
NSLog(@"person encodeWithCoder");
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeObject:self.photo forKey:@"photo"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self=[super init]) {
NSLog(@"person encodeWithCoder");
_age=[aDecoder decodeIntegerForKey:@"age"];
_name=[aDecoder decodeObjectForKey:@"name"];
_photo=[aDecoder decodeObjectForKey:@"photo"];
}
return self;
}
@end
在Student类中继承Person类 增加了一个学号属性
#import "Person.h"
@interface Student : Person
@property(nonatomic,strong)NSString *stuId;
@end
在Student.m中实现了NSCodeing协议
//
// Student.m
// Archive
//
// Created by City--Online on 15/4/22.
// Copyright (c) 2015年 CYW. All rights reserved.
//
#import "Student.h"
@implementation Student
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder:aCoder];
NSLog(@"student encodeWithCoder");
[aCoder encodeObject:self.stuId forKey:@"stuId"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self=[super initWithCoder:aDecoder]) {
NSLog(@"student initWithCoder");
_stuId=[aDecoder decodeObjectForKey:@"stuId"];
}
return self;
}
@end
自定义模型对象归档解档运行结果如下:
主要是实现NSCoding协议