//(main 接口) // ViewController.m // OC19歸檔 // // Created by Zoujie on 15/11/28. // Copyright © 2015年 Zoujie. All rights reserved. // #import "ViewController.h" #import "Foo.h" #import "AddressCard.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 歸檔基本的oc對象:NSString,NSArray,NSDictionary,NSSet,NSDate,NSNumber,NSData. // 寫入 [self writeTofile]; // 讀取 [self readTofile]; // 使用 NSKeyedArchiver 歸檔 [self Archiver]; // 歸檔自定義對象,必須添加<NSCoding>協議 [self addressCardEncode]; // 編碼解碼基本類型 [self Foo]; // 使用NSData [self useNSData]; // 使用歸檔實現深複製 [self makeNSKeyedArchivertoDeepCopy]; } -(void)writeTofile { //寫入 NSDictionary *glossary = @{@"abstract class":@"A class defined so other classes can inherit from it."}; if ([glossary writeToFile:@"glossary" atomically:YES] == NO)//直接寫入無效 { NSLog(@"Save to file failed"); } else{ NSLog(@"Save to file success"); } NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//獲取路徑,寫入有效 NSString *documentsDirectory = [paths objectAtIndex:0]; // NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"livefile.mp3"]; NSString *documents = [documentsDirectory stringByAppendingPathComponent:@"glossary"]; BOOL isosd = [glossary writeToFile:documents atomically:YES]; /* glossary <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>abstract class</key> <string>A class defined so other classes can inherit from it.</string> </dict> </plist> */ NSLog(@"%d",isosd); NSLog(@"%@",documentsDirectory);///Users/zoujie/Library/Developer/CoreSimulator/Devices/73E158C2-DEED-492D-9356-C455E46F9ED9/data/Containers/Data/Application/A8623DDD-8E74-43EB-BDAE-E53D8C73305A/Documents 路徑 } -(void)readTofile { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0];//獲取document 文件路徑 NSString *documents = [docDir stringByAppendingPathComponent:@"glossary"]; NSDictionary *glossary; glossary = [NSDictionary dictionaryWithContentsOfFile:documents]; NSLog(@"%@,%@",glossary,docDir); NSLog(@"%@",documents); for (NSString *key in glossary) { NSLog(@"%@",glossary[key]); } } -(void)Archiver { //歸檔 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0];//獲取document 文件路徑 NSDictionary *glossary = @{@"myBestLove":@"is a good girl"}; NSString *documents = [docDir stringByAppendingPathComponent:@"glossary"]; [NSKeyedArchiver archiveRootObject:glossary toFile:documents]; //讀取 NSDictionary *readGloaasry; readGloaasry = [NSKeyedUnarchiver unarchiveObjectWithFile:documents]; for (NSString *key in readGloaasry){ NSLog(@"%@:%@",key,readGloaasry[key]); } } -(void) addressCardEncode { NSString *name = @"Zou"; NSString *email = @"163.com"; AddressCard *card =[[AddressCard alloc]init]; [card setName:name andEmail:email]; if ([NSKeyedArchiver archiveRootObject:card toFile:@"addressCard.arch"] == NO) { NSLog(@"archiving failed"); } } -(void)Foo { Foo *myFoo1 = [[Foo alloc]init]; Foo *myFoo2 = [[Foo alloc]init]; myFoo1.strVal = @"You are my best Love"; myFoo1.intVal = 123; myFoo1.floatVal = 0.09; // myFoo2.strVal = @"Love you forever"; // myFoo2.intVal = 1010; // myFoo2.floatVal = 0.33; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0];//獲取document 文件路徑 NSString *documents = [docDir stringByAppendingPathComponent:@"foo.arch"]; [NSKeyedArchiver archiveRootObject:myFoo1 toFile:documents]; myFoo2 = [NSKeyedUnarchiver unarchiveObjectWithFile:documents]; NSLog(@"%@\n%i\n%g",myFoo2.strVal,myFoo2.intVal,myFoo2.floatVal); } -(void)useNSData { Foo *myFoo1 = [[Foo alloc]init]; NSMutableData *dataArea; NSKeyedArchiver *archiver; NSKeyedUnarchiver *unarchiver; AddressCard *myCard; myFoo1.strVal = @"M love"; myFoo1.intVal = 10; myFoo1.floatVal = 10.10; // 設置數據區,並將其鏈接到一個NSKeyedArchiver對象 dataArea = [NSMutableData data]; archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:dataArea]; // 如今開始存檔對象 [archiver encodeObject:myCard forKey:@"myaddresscard"]; [archiver finishEncoding]; // 將存檔的數據區寫到文件 if ([dataArea writeToFile:@"myArchive" atomically:YES] == NO) { NSLog(@"archiving failed"); } // 從文檔文件中讀取並鏈接 NSKeyedUnarchiver 對象 dataArea = [NSMutableData dataWithContentsOfFile:@"myArchive"]; if (! dataArea) { NSLog(@"Can't read back archiver file"); } unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:dataArea]; //解碼之前存儲在歸檔文件中的對象 myCard = [unarchiver decodeObjectForKey:@"myaddresscard"]; [unarchiver finishDecoding]; //驗證是否還原成功 NSLog(@"-----%@\n%i\n%g\n",myFoo1.strVal,myFoo1.intVal,myFoo1.floatVal); } -(void)makeNSKeyedArchivertoDeepCopy { NSData *data; NSMutableArray *dataArray = [NSMutableArray arrayWithObjects: [NSMutableString stringWithString:@"Zou"], [NSMutableString stringWithString:@"Jie"], [NSMutableString stringWithString:@"Lun"], nil]; NSMutableArray *dataArray2; NSMutableString *mStr; //使用歸檔器進行深層複製 data = [NSKeyedArchiver archivedDataWithRootObject:dataArray]; dataArray2 = [NSKeyedUnarchiver unarchiveObjectWithData:data]; mStr = dataArray2[0]; [mStr appendString:@"iOS"]; NSLog(@"dataArray:"); for (NSString *elem in dataArray) { NSLog(@"%@",elem); } NSLog(@"\ndataArray2:"); for (NSString *elem in dataArray2) NSLog(@"%@",elem); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
//接口 // AddressCard.h // OC19歸檔 // // Created by Zoujie on 15/11/29. // Copyright © 2015年 Zoujie. All rights reserved. // #import <Foundation/Foundation.h> @interface AddressCard : NSObject<NSCoding,NSCopying> @property (copy,nonatomic) NSString *name,*email; -(void) setName:(NSString *)theName andEmail:(NSString *)theEmail; -(NSComparisonResult) compareNames:(id) element; -(void) print; //添加NSCoding協議的方法 -(void) assignName:(NSString *) theName andEmail :(NSString *) theEmail; @end //實現文件 #import "AddressCard.h" @implementation AddressCard -(void)setName:(NSString *)theName andEmail:(NSString *)theEmail { _name = theName; _email = theEmail; } -(NSComparisonResult) compareNames:(id)element { return 1; } //協議自懂調用 解碼方法 -(void) encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_name forKey:@"AddressCardName"]; [aCoder encodeObject:_email forKey:@"AddressCardEmail"]; } -(id) initWithCoder:(NSCoder *)aDecoder { _name = [aDecoder decodeObjectForKey:@"AddressCardName"]; _email = [aDecoder decodeObjectForKey:@"AddressCardEmail"]; return self; } @end
// // Foo.h // OC19歸檔 // // Created by Zoujie on 15/11/29. // Copyright © 2015年 Zoujie. All rights reserved. // #import <Foundation/Foundation.h> //對基本數據類型的編碼解碼 @interface Foo : NSObject<NSCoding> @property (copy , nonatomic) NSString *strVal; @property int intVal; @property float floatVal; @end // // Foo.m // OC19歸檔 // // Created by Zoujie on 15/11/29. // Copyright © 2015年 Zoujie. All rights reserved. // #import "Foo.h" @implementation Foo @synthesize strVal , intVal , floatVal; -(void) encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:strVal forKey:@"FoostrVal"]; [aCoder encodeInt:intVal forKey:@"FoointVal"]; [aCoder encodeFloat:floatVal forKey:@"FoofloatVal"]; } -(id) initWithCoder:(NSCoder *)aDecoder { strVal = [aDecoder decodeObjectForKey:@"FoostrVal"]; intVal = [aDecoder decodeIntForKey:@"FoointVal"]; floatVal = [aDecoder decodeFloatForKey:@"FoofloatVal"]; return self; } @end