IOS階段學習第18天筆記(歸檔與解歸檔操作),ios第18天

來源:互聯網
上載者:User

IOS階段學習第18天筆記(歸檔與解歸檔操作),ios第18天

IOS學習(OC語言)知識點整理

一、歸檔與解歸檔的操作

 

1)歸檔是一個過程,將一個或多個Object Storage Service起來,以便以後可以還原,包括將對象存入檔案,以後再讀取

     將資料對象歸檔成plist檔案

 

2)plist檔案中只能存放:NSString、NSDate、NSNumber、Bool、NSData、NSArray、NSDictionary

     並且NSArray和NSDictionary中只能是以上的類型

 

3)歸檔存放時資料是什麼類型,讀取資料時就用什麼類型的資料接收。

 

4)歸檔不能直接操作自訂物件類型的資料。

 

5)歸檔與解歸檔操作執行個體代碼 :     

 1 //建立一個二維數組(數組中每個元素又是一個數組對象) 2 NSMutableArray *array1=[[NSMutableArray alloc]init]; 3 for(int i=0;i<4;i++){ 4   [array1 addObject:[NSString stringWithFormat:@"str%d",i+1]]; 5 } 6   7 NSMutableArray *array2=[[NSMutableArray alloc]init]; 8  for(int i=0;i<5;i++){ 9    [array2 addObject:[NSNumber numberWithInt:arc4random()%100]];10 }11 12 NSArray *bigArray=@[array1,array2];13 //將數組對象寫入檔案,(先寫入記憶體中,如果寫入成功,馬上存入檔案)14 [bigArray writeToFile:@"/Users/kingkong/Desktop/day08/array.plist" atomically:YES];15 16 //將plist檔案的內容直接讀取出存入數組17 NSArray *newArray=[[NSArray alloc]initWithContentsOfFile:@"/Users/kingkong/Desktop/day08/array.plist"];18 NSLog(@"%@",newArray);19 20 NSArray *emails=@[@"zhangsan@163.com",@"zhangsan@qq.com"];21 //建立一個字典對象22 NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"123456",@"password",emails,@"email", nil];23 //將字典對象寫入檔案24 [dict writeToFile:@"/Users/kingkong/Desktop/day08/dict.plist" atomically:YES];25 26  //將plist檔案的內如讀取出來存入字典27 NSDictionary *newDict=[NSDictionary dictionaryWithContentsOfFile:@"/Users/kingkong/Desktop/day08/dict.plist"];28 NSLog(@"%@",newDict);

6)歸檔與解歸檔自訂類資料 執行個體代碼:

    1、定義一個Birthday類,在.h檔案中遵守NSCoding 協議 例如: 

1 @interface Birthday : NSObject<NSCoding>2 //出生日期類,年、月、日3 @property(nonatomic,assign)int year;4 @property(nonatomic,assign)int month;5 @property(nonatomic,assign)int day;6 @end

 
   2、在.m檔案中實現NSCoding協議方法 例如: 

 1 #import "Birthday.h" 2 @implementation Birthday 3 //在歸檔時自動調用這個方法,將所有的成員變數編碼(給成員變數設定相應的鍵) -(void)encodeWithCoder:(NSCoder *)aCoder 4   { 5      [aCoder encodeInt:_year forKey:@"year"]; 6      [aCoder encodeInt:_month forKey:@"month"]; 7      [aCoder encodeInt:_day forKey:@"day"]; 8     } 9 10 -(id)initWithCoder:(NSCoder *)aDecoder11     {12     if(self=[super init]){13         _year=[aDecoder decodeIntForKey:@"year"];14         _month=[aDecoder decodeIntForKey:@"month"];15         _day=[aDecoder decodeIntForKey:@"day"];16     }17       return self;18    }19 @end

  

  3、在 main 檔案中執行歸檔與解歸檔方法 例如: 

 1 Birthday *b=[[Birthday alloc]init]; 2 b.year=2015; 3 b.month=10; 4 b.day=25; 5  6 //b必須遵守歸檔協議 7 NSString *path=@"/Users/kingkong/Desktop/day09/Birthday.data"; 8 //執行歸檔操作 9 BOOL ret=[NSKeyedArchiver archiveRootObject:b toFile:path];10 if(ret){11 //執行解歸檔操作12 Birthday *b2=[NSKeyedUnarchiver unarchiveObjectWithFile:path];13 NSLog(@"year:%i",b2.year);14 }

 
7)將多個對象歸檔到一個檔案中 執行個體代碼
   
 1、定義一個Person類 在.h檔案中遵守NSCoding協議 例如: 

1  #import <Foundation/Foundation.h>2   //如果要對對象進行歸檔,必須遵守歸檔協議,實現協議中規範的方法3   @interface Person : NSObject<NSCoding>4   @property(nonatomic,copy)NSString *name;5   @property(nonatomic,assign)int age;6   -(void)print;7   @end

  
 2、在.m中實現協議方法 例如: 

 1   #import "Person.h" 2    @implementation Person 3   //在歸檔時自動調用這個方法,將所有的成員變數編碼(給成員變數設定相應的鍵) 4   - (void)encodeWithCoder:(NSCoder *)aCoder 5   { 6     NSLog(@"%@",NSStringFromSelector(_cmd));
//encodeInt 用於整型資料 encodeObject 用於字串或對象 7 [aCoder encodeObject:_name forKey:@"name"]; 8 [aCoder encodeInt:_age forKey:@"age"]; 9 }10 //解歸檔時自動調用此方法11 - (id)initWithCoder:(NSCoder *)aDecoder12 {13 //如果父類也遵守了歸檔協議,self=[super initWithCode:aDecode]14 if(self=[super init]){15 //根據編碼時的鍵取值decodeIntForKey 用於整型資料 decodeObjectForKey 用於字串或對象16 _name=[aDecoder decodeObjectForKey:@"name"];17 _age=[aDecoder decodeIntForKey:@"age"];18 }19 return self;20 }21 -(void)print22 {23 NSLog(@"name:%@,age:%d",_name,_age);24 }25 @end

 

 3、在main檔案中執行方法 例如:

 1 Person *p1=[[Person alloc]init]; 2 p1.name=@"kingkong"; 3 p1.age=20; 4          5 NSArray *array1=@[@"red",@"blue",@"yellow"]; 6          7 //建立一個對象的緩衝區空間 8 NSMutableData *mutableData=[[NSMutableData alloc]init]; 9 //建立一個歸檔器,關聯一個對象的緩衝區10 NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData];11 //將對象編碼後存入緩衝區12 [archiver encodeObject:p1 forKey:@"person"];13 [archiver encodeObject:array1 forKey:@"array"];14 //編碼結束15 [archiver finishEncoding];//16 //將緩衝區中的資料寫入到檔案中17 NSString *path=@"/Users/kingkong/Desktop/day09/doc.data";18 BOOL ret=[mutableData writeToFile: path atomically:YES];19 NSLog(@"ret=%d",ret);20         21 //解歸檔操作22 NSData *data=[NSData dataWithContentsOfFile: path];23 //建立一個解歸檔器對象指定資料所在的緩衝區24 NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];25 //使用解歸檔器提取資料26 Person *p2=[unarchiver decodeObjectForKey:@"person"];27 NSArray *array2=[unarchiver decodeObjectForKey:@"array"];28 //解歸檔結束29 [unarchiver finishDecoding];30 NSLog(@"%@,%d",p2.name,p2.age);31 NSLog(@"%@",array2);

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.