码迷,mamicode.com
首页 > 其他好文 > 详细

归档 文件存储

时间:2015-12-21 16:05:26      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:

NSKeyedArchiver *a;  //打包 存入

 NSKeyedUnarchiver *b;//解包 取出

 

 

//
//  main.m
//  对象归档
//
//  Created by MAC on 15/12/21.
//  Copyright © 2015年 MAC. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 5.利用归档实现对象的深复制
        NSMutableString *s1 = [NSMutableString stringWithString:@"a"];
          NSMutableString *s2 = [NSMutableString stringWithString:@"b"];
          NSMutableString *s3 = [NSMutableString stringWithString:@"c"];
        
        NSMutableArray *array5 = [NSMutableArray arrayWithCapacity:5];
        [array5 addObject:s1];
        [array5 addObject:s2];
        [array5 addObject:s3];
        //返回的是二进制对象 没有经过文件
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array5];
        
        //解码二进制对象
        NSMutableArray *array6 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        //对数组中的对象进行修改 不会改变另一个数组
        [s1 insertString:@"changed" atIndex:0];
         NSLog(@"%@",array5);
        NSLog(@"%@",array6);
        
        
        
        // 4.归档自定义类型需要自己实现NSCoding协议和协议的方法
        Person *p1 = [[Person alloc]init];
        p1.pid = 1;
        p1.name = @"tom";
        p1.age = 20;
         NSString *path = @"/Users/mac/desktop/test.data";
        [NSKeyedArchiver archiveRootObject:p1 toFile:path];
        //报错 -[Person encodeWithCoder:]: unrecognized selector sent to instance没有实现协议  -> NSCoding
        /*
         
         
         @protocol NSCoding
         编码
         - (void)encodeWithCoder:(NSCoder *)aCoder;
         - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER
          解码
         @end
        
         2.NSCoder 编码器
         encodeXXXForKey数据类型编码
         decodeXXXForKey  解码
         
         */
        
        //解码
        Person *p2 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        NSLog(@"%d,%d,%@",p2.age,p2.pid,p2.name);
        
        
        
        
        
      // 3.NSKeyedArchiver *a;  //打包 存入
         NSKeyedUnarchiver *b;//解包 取出
        //NSKeyedArchiver核心方法
        //+ (NSData *)archivedDataWithRootObject:(id)rootObject;
        //+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;
         NSString *path = @"/Users/mac/desktop/test.data";
        NSString *str1 = @"这是NSKeyedArchiver数据";
        //压缩的形式存入文件
        [NSKeyedArchiver archiveRootObject:str1 toFile:path];
        NSString *str2 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        NSLog(@"%@",str2);
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];
        [dict setObject:@"one" forKey:@"1"];
        [dict setObject:@"two" forKey:@"2"];
       [dict setObject:@"three" forKey:@"3"];
        [dict setObject:@"four" forKey:@"4"];
        //字典压缩存入
        [NSKeyedArchiver archiveRootObject:dict toFile:path];
        //解压取出
        NSLog(@"%@",[NSKeyedUnarchiver unarchiveObjectWithFile:path]);
       
        
        //NSData 二进制的
        //归档 把对象的状态持久保存到文件系统(数据库、网络、云平台)
        Person *p = [[Person alloc]init];
        p.pid = 1;
        p.name = @"tom";
        p1.age = 20;
       
        //plist =Property List
        NSString *path = @"/Users/mac/desktop/test.txt";
        NSString *str = @"Hello World";
        //写入文件 atomically:YES 是否产生临时文件 保证数据的完整性
        [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
        //从文件读取出来
        str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@",str);
        NSArray *array = @[@"one",@"two",@"three"];
        [array writeToFile:path atomically:YES];
        
        NSArray *array1 = [NSArray arrayWithContentsOfFile:path];
        NSLog(@"%@",array1);
        
    }
    return 0;
}
//
//  Person.h
//  对象归档
//
//  Created by MAC on 15/12/21.
//  Copyright © 2015年 MAC. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject<NSCoding>
@property(nonatomic)int pid;
@property(nonatomic,copy)NSString *name;
@property(nonatomic)int age;
@end
//
//  Person.m
//  对象归档
//
//  Created by MAC on 15/12/21.
//  Copyright © 2015年 MAC. All rights reserved.
//

#import "Person.h"

@implementation Person
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeInt:self.pid forKey:@"pid"];
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInt:self.age forKey:@"age"];
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self.pid = [aDecoder decodeIntForKey:@"pid"];
    self.name = [aDecoder decodeObjectForKey:@"name"];
    self.age = [aDecoder decodeIntForKey:@"age"];
    return  self;
}
@end

 

归档 文件存储

标签:

原文地址:http://www.cnblogs.com/WJR12/p/5063433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!