IOS-Archiver檔案歸檔(2)

來源:互聯網
上載者:User

標籤:style   color   strong   檔案   資料   os   

Archiver是持久化資料的一種方式,他跟 Plist的區別在於他能持久化自訂對象。但他沒Plist那麼方便。

Archiver預設能持久化的資料有NSNumber,NSArray,NSDictionary,NSString,NSData,因為這幾個對象已經實現了

<NSCoding>協議。假設我們要實現一個對象的Archiver持久化 ,也必須實現該對象。


1.<NSCoding>協議主要為歸檔/恢複檔案兩個方法

//恢複歸檔檔案為對象-(id)initWithCoder:(NSCoder *)aDecoder//歸檔,使對象持久化-(void)encodeWithCoder:(NSCoder *)aCoder


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

如下 ,我們首先擷取歸檔檔案的路徑

#pragma mark 擷取檔案路徑- (NSString *) filePath{    NSArray *dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);    NSString *dirPath=dirPaths[0];    NSString *filePath=[dirPath stringByAppendingPathComponent:@"aa.archiver"];    return filePath;}


2.系統預設對象如何歸檔(NSNumber,NSArray,NSDictionary,NSString,NSData)

#pragma mark 歸檔/恢複 Array對象- (void) savearray{        NSString *filePath=[self filePath];////    NSArray *[email protected][@"ttt",@"BBB",@25];//    [NSKeyedArchiver archiveRootObject:arr toFile:filePath];//    NSArray *arr1=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];    NSLog(@"%@",arr1);}

#pragma mark 歸檔/恢複 Dictionary對象- (void) saveDic{    NSString *filePath=[self filePath];//    NSDictionary *[email protected]{@"name":@"lean",@"age":@25};//    BOOL flag=[NSKeyedArchiver archiveRootObject:dict toFile:filePath];//    NSLog(@"%d",flag);    NSDictionary *dict2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];    NSLog(@"%@",dict2);}

3.如何歸檔自訂對象。定義了一個Person類,如下:

#import <Foundation/Foundation.h>@interface Person : NSObject <NSCoding>@property (nonatomic,copy) NSString *name;@property (nonatomic,assign) int age;+ (Person *) initWithName:(NSString *)name andAge:(int) age;@end#import "Person.h"@implementation Person+ (Person *) initWithName:(NSString *)name andAge:(int) age{    Person *p=[[Person alloc] init];    p.name=name;    p.age=age;    return p;}-(void)encodeWithCoder:(NSCoder *)aCoder{    [aCoder encodeObject:self.name forKey:@"name"];    [aCoder encodeInt:self.age forKey:@"age"];}-(id)initWithCoder:(NSCoder *)aDecoder{    [self setName:[aDecoder decodeObjectForKey:@"name"]];    [self setAge:[aDecoder decodeIntForKey:@"age"]];    return self;}@end
TIP: 不管是encode還是decode 都是根據對象的類型去選用不同的方法。如

encodeInt:forkey:      encodeDouble:forkey:   encodeFloat:forkey: 

decodeObjectForKey:  decodeIntForKey:  decodeDoubleForKey:


NSKeyedArchiver archiveRootObject:toFile:

NSKeyedUnarchiver unarchiveObjectWithFile:  

分別是對需要歸檔。恢複的對象進行操作的兩個類


定義完了Person類後,在需要歸檔的地方調用如下:

#pragma mark 歸檔/恢複 自訂對象- (void) savePerson{    NSString *filePath=[self filePath];    Person *p=[Person initWithName:@"lean" andAge:22];    BOOL flag=[NSKeyedArchiver archiveRootObject:p toFile:filePath];    Person *p2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];    NSLog(@"%d-%d",flag,p2.age);}

對於其Person類,假設該類中還有自訂對象作為屬性,同樣實現<NSCoding>協議


4.假設該對象是某個對象子類,這裡我們建立一個叫Student類作為Person的子類


#import "Person.h"@interface Student : Person@property (nonatomic ,assign) int no;+ (Student *) initWithName:(NSString *)name andAge:(int) age andNO:(int) no;@end

同樣Student也需要實現NSCoding協議的方法

-(id)initWithCoder:(NSCoder *)aDecoder{    if (self=[super initWithCoder:aDecoder]) {        [self setNo:[aDecoder decodeIntForKey:@"no"]];    }    return self;}-(void)encodeWithCoder:(NSCoder *)aCoder{    [super encodeWithCoder:aCoder];    [aCoder encodeInt:self.no forKey:@"no"];}

#pragma mark 歸檔/恢複 自訂子類對象- (void) saveStudent{    NSString *filePath=[self filePath];    Student *p=[Student initWithName:@"lean" andAge:22 andNO:150133];    BOOL flag=[NSKeyedArchiver archiveRootObject:p toFile:filePath];    Student *p2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];    NSLog(@"%d-%@",flag,p2.name);}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.