歸檔的一些小步驟

//  Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject <NSCoding>

@property (nonatomic,assign) int idNum;
@property (nonatomic,copy) NSString *name;

- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;

@end


//  Student.m

#import "Student.h"

#define IDNUM @"idNum"
#define kName @"name"

@implementation Student
#pragma mark 編碼 對對象屬性進行編碼的處理
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeInt:_idNum forKey:IDNUM];
    [aCoder encodeObject:_name forKey:kName];
}
#pragma mark 解碼 解碼歸檔數據來初始化對象
- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self == [super init]) {
       _idNum = [aDecoder decodeIntegerForKey:IDNUM];
       _name = [aDecoder decodeObjectForKey:kName];
    }
    return self;
}

- (NSString*)description {
    return [NSString stringWithFormat:@"idNum = %i  name = %@",_idNum,_name];
}


@end

<pre name="code" class="objc">//  ViewController.m
//  歸檔


#import "ViewController.h"
#import "Student.h"

@interface ViewController ()

@end

@implementation ViewController

#pragma mark 複習
- (void)plistAndUserDefaultRevise {
    //動態創建一個plist文件
    //得到路徑
    //屬性列表(存到document目錄裏面)
    //    NSDictionary *dic = @{@"idNum":[NSNumber numberWithInt:1],@"name":@"bendan"};
    //    if ([dic writeToFile:filePath atomically:YES]) {
    //        NSLog(@"存儲成功........save ok!");
    //    }
    
    //userdefault(存到library裏面)
    //    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //    [defaults setObject:@"benJiDan" forKey:@"name"];
    //    [defaults synchronize];
}
#pragma mark 自定義內容歸檔
- (void)archvierByMyself {
    //自定義內容歸檔
    NSString *filePathTwo = [self getDocumentAppendingPathByFileName:@"filePathTwo.archiver"];
    //1. 使用NSData存放歸檔數據
    NSMutableData *data = [NSMutableData data];
    //2. 創建歸檔對象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //3. 添加歸檔內容 (設置鍵值對)
    [archiver encodeObject:@"jereh" forKey:@"name"];
    [archiver encodeInt:20 forKey:@"age"];
    [archiver encodeObject:@[@"benDan",@"daBenDan"] forKey:@"zhangziyao"];
    //4. 完成歸檔
    [archiver finishEncoding];
    
    //5. 將歸檔信息存儲到磁盤上
    if ([data writeToFile:filePathTwo atomically:YES]) {
        NSLog(@"存儲笨蛋完畢>>.....<<");
    }
    //=========================================================
    //解歸檔
    //1. 從磁盤讀取文件,生成NSData實例
    NSData *unarchiverData = [NSData dataWithContentsOfFile:filePathTwo];
    //2. 根據Data實例創建和初始化解歸檔對象
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:unarchiverData];
    //3. 解歸檔,根據key值訪問
    NSString *name = [unarchiver decodeObjectForKey:@"name"];
    int age = [unarchiver decodeIntegerForKey:@"age"];
    NSArray *nameAry = [unarchiver decodeObjectForKey:@"zhangziyao"];
    NSLog(@"name = %@ ,age = %i , nameAry = %@ ",name,age,nameAry);
}
#pragma mark 歸檔對象
- (void)archiverModel {
    NSString *studentPath = [self getDocumentAppendingPathByFileName:@"student.archiver"];
    
    //1. 使用NSData存放歸檔數據
    NSMutableData *data = [NSMutableData data];
    //2. 創建歸檔對象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //3. 添加歸檔內容
    
    Student *stu1 = [Student new];
    stu1.idNum = 1;
    stu1.name = @"shaGua";
    Student *stu2 = [Student new];
    stu2.idNum = 2;
    stu2.name = @"小青年";
    
    
    [archiver encodeObject:stu1 forKey:@"studentOne"];
    [archiver encodeObject:stu2 forKey:@"studentTwo"];
    //4. 完成歸檔
    [archiver finishEncoding];
    
    //5. 將歸檔信息存儲到磁盤上
    if ([data writeToFile:studentPath atomically:YES]) {
        NSLog(@"存儲笨蛋完畢>>.....<<");
    }
    //=========================================================
    //解歸檔
    //1. 從磁盤讀取文件,生成NSData實例
    NSData *unarchiverData = [NSData dataWithContentsOfFile:studentPath];
    //2. 根據Data實例創建和初始化解歸檔對象
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:unarchiverData];
    //3. 解歸檔,根據key值訪問
    Student *UnarchiverStudent1 = [unarchiver decodeObjectForKey:@"studentOne"];
    
    Student *UnarchiverStudent2 = [unarchiver decodeObjectForKey:@"studentTwo"];
    NSLog(@" %@ \n %@ ",UnarchiverStudent1
          ,UnarchiverStudent2);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getDocumentAppendingPathByFileName:@"class.plist"];
}
- (NSString*)getDocumentAppendingPathByFileName:(NSString*)fileName {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //追加文件名稱
    NSString *filePath = [documentPath stringByAppendingPathComponent:fileName];
    NSLog(@"filePath===========%@",filePath);
    return filePath;
}
#pragma mark //解歸檔
- (void)texstOne {
    NSString *fileOne = [self getDocumentAppendingPathByFileName:@"fileOne.archiver"];
    //歸檔(序列化)
    NSArray *aryOne = @[@"jereh",@"ios"];
    
    if ([NSKeyedArchiver archiveRootObject:aryOne toFile:fileOne]) {
        NSLog(@"歸檔序列化成功........");
    }
    
    //解歸檔(反序列化)
    NSArray *unarChiverArrayOne =  [NSKeyedUnarchiver unarchiveObjectWithFile:fileOne];
    NSLog(@"unarChiverArrayOne=======%@",unarChiverArrayOne);
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end




發佈了52 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章