国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

iOS階段學(xué)習(xí)第18天筆記(Plist-Archiver-歸檔與解歸檔操作)

2019-11-14 18:43:05
字體:
供稿:網(wǎng)友

iOS學(xué)習(xí)(OC語言)知識點整理

一、歸檔與解歸檔的操作

 

1)歸檔是一個過程,將一個或多個對象存儲起來,以便以后可以還原,包括將對象存入文件,以后再讀取

     將數(shù)據(jù)對象歸檔成plist文件

 

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

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

 

3)歸檔存放時數(shù)據(jù)是什么類型,讀取數(shù)據(jù)時就用什么類型的數(shù)據(jù)接收。

 

4)歸檔不能直接操作自定義對象類型的數(shù)據(jù)。

 

5)歸檔與解歸檔操作實例代碼 :     

 1 //創(chuàng)建一個二維數(shù)組(數(shù)組中每個元素又是一個數(shù)組對象) 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 //將數(shù)組對象寫入文件,(先寫入內(nèi)存中,如果寫入成功,馬上存入文件)14 [bigArray writeToFile:@"/Users/kingkong/Desktop/day08/array.plist" atomically:YES];15 16 //將plist文件的內(nèi)容直接讀取出存入數(shù)組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 //創(chuàng)建一個字典對象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文件的內(nèi)如讀取出來存入字典27 NSDictionary *newDict=[NSDictionary dictionaryWithContentsOfFile:@"/Users/kingkong/Desktop/day08/dict.plist"];28 NSLog(@"%@",newDict);

6)歸檔與解歸檔自定義類數(shù)據(jù) 實例代碼:

    1、定義一個Birthday類,在.h文件中遵守NSCoding 協(xié)議 例如: 

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文件中實現(xiàn)NSCoding協(xié)議方法 例如: 

 1 #import "Birthday.h" 2 @implementation Birthday 3 //在歸檔時自動調(diào)用這個方法,將所有的成員變量編碼(給成員變量設(shè)置相應(yīng)的鍵) -(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 文件中執(zhí)行歸檔與解歸檔方法 例如: 

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

 
7)將多個對象歸檔到一個文件中 實例代碼
   
 1、定義一個Person類 在.h文件中遵守NSCoding協(xié)議 例如: 

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

  
 2、在.m中實現(xiàn)協(xié)議方法 例如: 

 1   #import "Person.h" 2    @implementation Person 3   //在歸檔時自動調(diào)用這個方法,將所有的成員變量編碼(給成員變量設(shè)置相應(yīng)的鍵) 4   - (void)encodeWithCoder:(NSCoder *)aCoder 5   { 6     NSLog(@"%@",NSStringFromSelector(_cmd));
//encodeInt 用于整型數(shù)據(jù) encodeObject 用于字符串或?qū)ο?/span> 7 [aCoder encodeObject:_name forKey:@"name"]; 8 [aCoder encodeInt:_age forKey:@"age"]; 9 }10 //解歸檔時自動調(diào)用此方法11 - (id)initWithCoder:(NSCoder *)aDecoder12 {13 //如果父類也遵守了歸檔協(xié)議,self=[super initWithCode:aDecode]14 if(self=[super init]){15 //根據(jù)編碼時的鍵取值decodeIntForKey 用于整型數(shù)據(jù) decodeObjectForKey 用于字符串或?qū)ο?/span>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文件中執(zhí)行方法 例如:

 1 Person *p1=[[Person alloc]init]; 2 p1.name=@"kingkong"; 3 p1.age=20; 4          5 NSArray *array1=@[@"red",@"blue",@"yellow"]; 6          7 //創(chuàng)建一個對象的緩沖區(qū)空間 8 NSMutableData *mutableData=[[NSMutableData alloc]init]; 9 //創(chuàng)建一個歸檔器,關(guān)聯(lián)一個對象的緩沖區(qū)10 NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData];11 //將對象編碼后存入緩沖區(qū)12 [archiver encodeObject:p1 forKey:@"person"];13 [archiver encodeObject:array1 forKey:@"array"];14 //編碼結(jié)束15 [archiver finishEncoding];//16 //將緩沖區(qū)中的數(shù)據(jù)寫入到文件中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 //創(chuàng)建一個解歸檔器對象指定數(shù)據(jù)所在的緩沖區(qū)24 NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];25 //使用解歸檔器提取數(shù)據(jù)26 Person *p2=[unarchiver decodeObjectForKey:@"person"];27 NSArray *array2=[unarchiver decodeObjectForKey:@"array"];28 //解歸檔結(jié)束29 [unarchiver finishDecoding];30 //NSLog(@"%@,%d",p2.name,p2.age);
31 [p2 print];
32 NSLog(@"%@",array2);

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 巨鹿县| 大化| 务川| 永嘉县| 龙里县| 大渡口区| 岳阳市| 留坝县| 光泽县| 淳化县| 柏乡县| 樟树市| 天全县| 丘北县| 永安市| 巨野县| 利津县| 汶川县| 海宁市| 岐山县| 济宁市| 天长市| 恭城| 天祝| 邹平县| 丽江市| 海林市| 蓝山县| 绍兴市| 汝南县| 黄平县| 尖扎县| 榆社县| 赤峰市| 肃北| 湖北省| 湖北省| 新野县| 米林县| 永和县| 苏尼特右旗|