OC19归档

简介:
//(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
相关文章
|
1月前
ApacheHudi Archive(归档)实现分析
ApacheHudi Archive(归档)实现分析
22 0
|
安全 测试技术
安全参考和书安归档
版权声明:License CC BY-NC-SA 4.0 https://blog.csdn.net/wizardforcel/article/details/82588261 《安...
1171 0
|
SQL Oracle 关系型数据库
Oracle归档模式和非归档模式
Oracle归档模式和非归档模式 解释归档和非归档模式之间的不同和它们各自的优缺点? 答:归档模式是指可以备份所有的数据库transactions并恢复到任意一个时间点。
1386 0
|
数据库 关系型数据库 Oracle
删除归档出现ORA-15028错误
在10.2.0.4 RAC环境中使用RMAN删除归档报错ORA-15028。     错误信息如下: RMAN> delete archivelog all completed before 'sysd...
1276 0
|
NoSQL 调度 索引
Mongorestore的archive(归档)模式恢复原理解析
在上篇Mongodump的archive(归档)模式原理解析中介绍过,Mongodump的archive(归档)模式产生的文件是将多个集合的数据通过一个Multiplexer多路复用混合在一起,因此对应在恢复的时候就需要有一个Demultiplexer来将数据进行解析,是一个多路复用的逆过程。对应.
5574 0