数据存储之归档

简介:
在使用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协议



相关文章
|
8天前
|
存储 Oracle 关系型数据库
服务器数据恢复—Storwize V3700存储数据恢复案例
**服务器存储数据恢复环境:** 某品牌Storwize V3700存储,10块硬盘组建了2组Mdisk加入到一个存储池中,一共创建了1个通用卷来存放数据,主要数据为oracle数据库。 **服务器存储故障:** 其中一组Mdisk中两块磁盘出现故障离线,该组Mdisk失效,导致该通用卷无法使用。
|
12月前
|
存储 对象存储
归档存储
归档存储
259 0
|
存储 人工智能 大数据
阿里云对象存储OSS标准存储、低频访问、归档和冷归档区别对比
阿里云对象存储OSS的Bucket存储类型标准存储、低频访问存储、归档存储和冷归档存储有什么区别?如何选择?
3051 1
阿里云对象存储OSS标准存储、低频访问、归档和冷归档区别对比
|
消息中间件 存储 XML
冷归档数据恢复最佳实践
对象存储OSS冷归档对象的读取需要先恢复出来,才可以读取,本文从用户角度介绍整个恢复的操作过程。
870 1
冷归档数据恢复最佳实践
|
SQL 存储 分布式数据库
HBaseOnOSS冷数据存储
本期直播资料下载以及往期直播资料下载大全
2105 0
|
关系型数据库 数据库
归档管理
一、查看归档模式 ARCHIVE LOG LIST; 二、开启归档 1、重启数据库至 mount 状态 alter database archivelog; 2、设置归档存放路径(只可以设置其一,不可以并行)   a) log_archive_dest:指定归档文件存放的路径,该路径只能是本地磁盘,默认为'',不能与以下参数同使用。
989 0