iOS归档,NSCoding协议

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS归档,NSCoding协议相关的知识,希望对你有一定的参考价值。

参考技术A 1.归档是将数据持久化的一种方式,一般针对于比较复杂对象,比如自定义的对象,来进行数据持久化操作。

2.归档的对象需要遵循NSCoding协议,存储的时候调用encodeWithCoder:方法,读取的时候调用initWithCoder:方法。

3.将数据写入本地需要调用 [NSKeyedArchiver archiveRootObject:per toFile:filePath],filePath为存储的路径。

4.从本地读取数据需要调用[NSKeyedUnarchiver unarchiveObjectWithFile:filePath],filePath为存储的路径。

1.自定义Person类服从NSCoding协议

#import<Foundation/Foundation.h>

#import<UIKit/UIKit.h>

@interfacePerson:NSObject

@property(nonatomic,copy)NSString* name;

@property(nonatomic,assign)int age;

@property(nonatomic,assign)CGFloat height;

@end

2.实现协议方法encodeWithCoder:和initWithCoder:

#import"Person.h"

@implementation Person

//写入文件时调用 -- 将需要存储的属性写在里面- (void)encodeWithCoder:(NSCoder*)aCoder

 [aCoder encodeObject:self.name forKey:@"name"]; 

 [aCoder encodeInt:self.age forKey:@"age"]; 

 [aCoder encodeFloat:self.height forKey:@"height"];



//从文件中读取时调用 -- 将需要存储的属性写在里面

- (nullable instancetype)initWithCoder:(NSCoder*)aDecoder 



      if(self= [super init])

                     self.name = [aDecoder decodeObjectForKey:@"name"];

                    self.age = [aDecoder decodeIntForKey:@"age"];

                    self.height = [aDecoder decodeFloatForKey:@"height"]; 

           

             return. self;



@end

3.写入与读取

写入:[NSKeyedArchiver archiveRootObject:per toFile:filePath]

读取: [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]

#import"ViewController.h"

#import"Person.h"

@interface ViewController()

@end

@implementation ViewController

- (void)viewDidLoad

 [superview DidLoad]; 

 [self saveModel]; 

 [self readModel];

     

//存储数据

- (void)saveModel  

 Person *per = [Person new];

 per.name =@"xiaoming";

 per.age =18; 

 per.height =180;

NSString*filePath = [self getFilePath];

//将对象per写入

[NSKeyedArchiverarchiveRootObject:per toFile:filePath]; 

 

//读取数据

- (void)readModel

NSString*filePath = [self getFilePath];

//取出per对象

Person *per = [NSKeyedUnarchiverunarchiveObjectWithFile:filePath];

NSLog(@"%@ -- %@ -- %d -- %.0f", per, per.name, per.age, per.height);



//获得全路径

- (NSString*)getFilePath

//获取

DocumentsNSString*doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];

//在Documents下创建Person.data文件

NSString*filePath = [doc stringByAppendingPathComponent:@"Person.data"];returnfilePath;



@end

iOS序列化-归档

实现<NSCoding>协议


#import "HMCThread.h"
#import <objc/message.h>

@implementation HMCThread

- (void)dealloc
    NSLog(@"HMCThread---dealloc");


- (void)encodeWithCoder:(NSCoder *)aCoder
    unsigned int outCount = 0;
    Ivar *vars = class_copyIvarList([self class], &outCount);
    for(int i=0; i<outCount; i++)
        Ivar var = vars[i];
        NSString *key = [NSString stringWithUTF8String:ivar_getName(var)];
        id value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    


- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
    if(self = [super init])
        unsigned int outCount = 0;
        Ivar *vars = class_copyIvarList([self class], &outCount);
        for(int i=0; i<outCount; i++)
            Ivar var = vars[i];
            NSString *key = [NSString stringWithUTF8String:ivar_getName(var)];
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        
    
    
    return self;

以上是关于iOS归档,NSCoding协议的主要内容,如果未能解决你的问题,请参考以下文章

NSCoding归档

iOS 文件操作--归档和解档

iOS 对模型对象进行归档

使用 nscoding 归档 UIVIew 及其子视图

IOS-Archiver文件归档

如何归档 NSDictionary 的 NSArray(使用 NSCoding)?