複雜數據的寫入文件夾

複雜對象的寫入文件
自己創建出來的類(例如 Student)
寫入的核心思想:
把複雜對象 轉化成 簡單對象 進行寫入
一般轉化成 NSData對象進行寫入

-----------------------------------------------------------------------

Student.h

@interface Student : NSObject<NSCoding>

   
/*
    
複雜對象寫入步驟:
        1.
遵守 NSCoding(需要在寫入的時候  按照一定的規則進行寫入 一定的編碼格式去寫入)歸檔與反歸檔(序列與反序列)
        2.
重寫(協議中) 歸檔 反歸檔方法
    
        3.
創建一個複雜對象出來
    
        4.
創建一個歸檔對象
    
        5.
利用歸檔對象 把複雜對象 轉化成Data
       
        6.
data寫入文件 進行數據持久化
     */


@property (nonatomic,retain)NSString *name;

@property (nonatomic,assign)NSInteger age;

@property (nonatomic,retain)NSData *imageData;

-----------------------------------------------------------------------
Student.m
@implementation Student

- (
void)dealloc
{
    [
_name release];
    [
_imageData release];
    [
super dealloc];
}

歸檔方法
- (
void)encodeWithCoder:(NSCoder *)aCoder
{
   
//按照一定編碼格式歸檔
   
//注意:歸檔時候的key 要與 反歸檔時的key一致
    [aCoder
encodeObject:self.name forKey:@"name"];
   
//要選用 與屬性一致的 編碼方法
    [aCoder
encodeInteger:self.age forKey:@"age"];
   
   
//一般統一使用對象類型的
    [aCoder
encodeObject:self.imageData forKey:@"imageData"
];
}

//反歸檔方法
- (
instancetype)initWithCoder:(NSCoder *)aDecoder
{
   
self = [super init];
   
if (self) {
       
//按照一定的編碼格式  和存儲的key標識 取出被歸檔的對象(反歸檔)
       
self.name = [aDecoder decodeObjectForKey:@"name"];
       
self.age = [aDecoder decodeIntegerForKey:@"age"];
        self.imageData = [aDecoder decodeObjectForKey:@"imageData"];
    }
    return self;
}

@end
-----------------------------------------------------------------------


- (void)viewDidLoad {
    [
super viewDidLoad];
   
// Do any additional setup after loading the view.
   
   
//複雜對象的寫入文件
   
//自己創建出來的類(例如 Student)
   
//寫入的核心思想:
   
//把複雜對象 轉化成 簡單對象 進行寫入
   
//一般轉化成 NSData對象進行寫入

   
   
//創建複雜對象
    Student *stu= [[Student alloc] init];
    複雜對象賦值
    stu.name = @"張三";
    stu.
age = 23;
    stu.
imageData = UIImagePNGRepresentation([UIImage imageNamed:@"32.jpg"]);
   
    NSMutableData *data = [NSMutableData data];(這時只是初始化 裏面還沒有值)
   
   
創建一個歸檔對象
   
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  
   
將複雜對象 通過歸檔對象 轉化成data
    [archiver
encodeObject:stu forKey:@"student"];
   
完成歸檔
    [archiver
finishEncoding];(這一步必須要寫)
   
   
實際上 通過歸檔對象 把複雜對象 寫入data
   
並且寫入的標識 key
   
   
注意:
   
歸檔與反歸檔 不是數據持久化
   
只是把複雜對象 按照系統編碼格式 轉化成data
    真正數據持久化 還是寫入文件
    NSLog(@"%@",data);(data裏面已經有值)
   
   
獲取路徑
   
NSString *documentpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
   
NSLog(@"%@",documentpath);
   
   
拼接路徑
   
NSString *path = [documentpath stringByAppendingPathComponent:@"/SaveStudent"];
   
    寫入文件
    [data
writeToFile:path atomically:YES];
   
釋放歸檔對象
    [archiver
release];
   
   
   -----------------------------------------------------------------------
    //反歸檔
   
//從沙盒中讀取data數據
   
NSData *dataNew = [NSData dataWithContentsOfFile:path];
   
   
//創建反歸檔對象(利用data創建)
   
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc ] initForReadingWithData:dataNew];
   
//利用反歸檔對象 反歸檔出複雜對象
   
Student *student = [unarchiver decodeObjectForKey:@"student"];
   
//調用完成  返回歸檔
    [unarchiver
finishDecoding];
   
   
UIImage *image = [UIImage imageWithData:student.imageData];
   
   
//如何把一個數組的 複雜對象 寫到文件中
   
//遍歷這個數組 把每一個複雜對象 轉化成簡單對象(data) 然後 再將data保存到數組中
   
//最後 直接把數組寫入文件中 進行持久化
   
   
}


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