//(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