iOS學(xué)習(xí)(OC語言)知識(shí)點(diǎn)整理
一、歸檔與解歸檔的操作
1)歸檔是一個(gè)過程,將一個(gè)或多個(gè)對(duì)象存儲(chǔ)起來,以便以后可以還原,包括將對(duì)象存入文件,以后再讀取
將數(shù)據(jù)對(duì)象歸檔成plist文件
2)plist文件中只能存放:NSString、NSDate、NSNumber、Bool、NSData、NSArray、NSDictionary
并且NSArray和NSDictionary中只能是以上的類型
3)歸檔存放時(shí)數(shù)據(jù)是什么類型,讀取數(shù)據(jù)時(shí)就用什么類型的數(shù)據(jù)接收。
4)歸檔不能直接操作自定義對(duì)象類型的數(shù)據(jù)。
5)歸檔與解歸檔操作實(shí)例代碼 :
1 //創(chuàng)建一個(gè)二維數(shù)組(數(shù)組中每個(gè)元素又是一個(gè)數(shù)組對(duì)象) 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ù)組對(duì)象寫入文件,(先寫入內(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=@[@"[email protected]",@"zhangsan@QQ.com"];21 //創(chuàng)建一個(gè)字典對(duì)象22 NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"123456",@"passWord",emails,@"email", nil];23 //將字典對(duì)象寫入文件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ù) 實(shí)例代碼:
1、定義一個(gè)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文件中實(shí)現(xiàn)NSCoding協(xié)議方法 例如:
1 #import "Birthday.h" 2 @implementation Birthday 3 //在歸檔時(shí)自動(dòng)調(diào)用這個(gè)方法,將所有的成員變量編碼(給成員變量設(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)將多個(gè)對(duì)象歸檔到一個(gè)文件中 實(shí)例代碼
1、定義一個(gè)Person類 在.h文件中遵守NSCoding協(xié)議 例如:
1 #import <Foundation/Foundation.h>2 //如果要對(duì)對(duì)象進(jìn)行歸檔,必須遵守歸檔協(xié)議,實(shí)現(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中實(shí)現(xiàn)協(xié)議方法 例如:
1 #import "Person.h" 2 @implementation Person 3 //在歸檔時(shí)自動(dòng)調(diào)用這個(gè)方法,將所有的成員變量編碼(給成員變量設(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 //解歸檔時(shí)自動(dòng)調(diào)用此方法11 - (id)initWithCoder:(NSCoder *)aDecoder12 {13 //如果父類也遵守了歸檔協(xié)議,self=[super initWithCode:aDecode]14 if(self=[super init]){15 //根據(jù)編碼時(shí)的鍵取值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)建一個(gè)對(duì)象的緩沖區(qū)空間 8 NSMutableData *mutableData=[[NSMutableData alloc]init]; 9 //創(chuàng)建一個(gè)歸檔器,關(guān)聯(lián)一個(gè)對(duì)象的緩沖區(qū)10 NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData];11 //將對(duì)象編碼后存入緩沖區(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)建一個(gè)解歸檔器對(duì)象指定數(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);
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注