IOS的归档小总结

时间:2021-10-31 16:21:47

1、什么是归档?

归档最通用的定义是存储有组织的数据。归档的目的是长时间存放有组织的数据集,确保其将来能够被精细地检索。


2、归档方法和属性的介绍

    /**

     实例化一个归档对象的方法

     1init

     NSKeyedArchiver * NetWorkArchiver = [[NSKeyedArchiver alloc]init];

     

     2+ (NSData *)archivedDataWithRootObject:(id)rootObject

     通过归档将数据转化成NSData类型

     NSData * data  = [NSKeyedArchiver archivedDataWithRootObject:@[@"SSS"]];

     

     3+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;

     将数据对象进行归档

     BOOL isFinish = [NSKeyedArchiver archiveRootObject:fastArray toFile:homePath];


     4- (instancetype)initForWritingWithMutableData:(NSMutableData *)data;

     使用 data初始化归档对象

     NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

     

     5、归档的代理

     @property (nullable, assign) id <NSKeyedArchiverDelegate> delegate;

     归档的代理

     1》数据将要归档的时候调用此函数

     - (nullable id)archiver:(NSKeyedArchiver *)archiver willEncodeObject:(id)object;

     

     2》归档中调用的函数

     - (void)archiver:(NSKeyedArchiver *)archiver didEncodeObject:(nullable id)object;


     3》归档数据替换的时候调用此函数

     - (void)archiver:(NSKeyedArchiver *)archiver willReplaceObject:(nullable id)object withObject:(nullable id)newObject;


     4》归档将要完成

     - (void)archiverWillFinish:(NSKeyedArchiver *)archiver;

     

     5归档完成

     - (void)archiverDidFinish:(NSKeyedArchiver *)archiver;


     6、归档输出的类型

     @property NSPropertyListFormat outputFormat;

     NSPropertyListFormat 的类型

     NSPropertyListOpenStepFormat = kCFPropertyListOpenStepFormat,//明文的方式

     NSPropertyListXMLFormat_v1_0 = kCFPropertyListXMLFormat_v1_0,//这个是xml的格式

     NSPropertyListBinaryFormat_v1_0 = kCFPropertyListBinaryFormat_v1_0//这个是二进制的格式

     

     7- (void)finishEncoding;

     归档完成

     

     8、归档的类型

     id 类型

     - (void)encodeObject:(nullable id)objv forKey:(NSString *)key;

     id 有条件的类型

     - (void)encodeConditionalObject:(nullable id)objv forKey:(NSString *)key;

     

     布尔值

     - (void)encodeBool:(BOOL)boolv forKey:(NSString *)key;

     

     int类型

     - (void)encodeInt:(int)intv forKey:(NSString *)key;// native int

     

     int32_t

     - (void)encodeInt32:(int32_t)intv forKey:(NSString *)key;

     

     int64_t

     - (void)encodeInt64:(int64_t)intv forKey:(NSString *)key;

     

     浮点类型

     - (void)encodeFloat:(float)realv forKey:(NSString *)key;

     

     双精度类型

     - (void)encodeDouble:(double)realv forKey:(NSString *)key;

     

     字节类型

     - (void)encodeBytes:(nullable const uint8_t *)bytesp length:(NSUInteger)lenv forKey:(NSString *)key;

     

     举例子:

     [archiver encodeObject:nameArray forKey:@"nameArray"];

     

     9@property (readwrite) BOOL requiresSecureCoding NS_AVAILABLE(10_8, 6_0);

     归档是否安全

     

     */


3、反归档的属性和介绍

    /**

     

     1+ (nullable id)unarchiveObjectWithData:(NSData *)data;

     通过二进制数据创建解归档对象

     

     

     2+ (nullable id)unarchiveObjectWithFile:(NSString *)path;

     通过文件路径创建解归档对象

     

     3- (instancetype)initForReadingWithData:(NSData *)data;

     读去二进制流创建解归档对象

     

     4- (void)finishDecoding;

     解归档完成

     

     5@property (nullable, assign) id <NSKeyedUnarchiverDelegate> delegate;

     解归档的代理

     

     1解归档的时候被调用

     - (nullable id)unarchiver:(NSKeyedUnarchiver *)unarchiver didDecodeObject:(nullable id) NS_RELEASES_ARGUMENT object NS_RETURNS_RETAINED;

     

     2解归档数据替换调用此函数

     - (void)unarchiver:(NSKeyedUnarchiver *)unarchiver willReplaceObject:(id)object withObject:(id)newObject;

 

     3解归档将要完成

     - (void)unarchiverWillFinish:(NSKeyedUnarchiver *)unarchiver;


     4》解归档完成

     - (void)unarchiverDidFinish:(NSKeyedUnarchiver *)unarchiver;


     6、检测归档中是否包含某个对象

     - (BOOL)containsValueForKey:(NSString *)key;


     7、解归档的方式

     id 对象的解归档

     - (nullable id)decodeObjectForKey:(NSString *)key;

     

     布尔值对象的解归档

     - (BOOL)decodeBoolForKey:(NSString *)key;

     

     Int 对象的解归档

     - (int)decodeIntForKey:(NSString *)key;

     

     int32_t 对象的解归档

     - (int32_t)decodeInt32ForKey:(NSString *)key;

     

     int64_t 对象的解归档

     - (int64_t)decodeInt64ForKey:(NSString *)key;

     

     float 对象的解归档

     - (float)decodeFloatForKey:(NSString *)key;

     

     double 对象的解归档

     - (double)decodeDoubleForKey:(NSString *)key;

     

     字节对象的解归档

     - (nullable const uint8_t *)decodeBytesForKey:(NSString *)key returnedLength:(nullable NSUInteger *)lengthp NS_RETURNS_INNER_POINTER;

     

     

     8、归档失败的类型

     @property (readwrite) NSDecodingFailurePolicy decodingFailurePolicy NS_AVAILABLE(10_11, 9_0);


     */



4、单个对象的归档

    NSArray * fastArray =@[@"SS",@"ZZ"];

    NSString * homeDictionart =NSHomeDirectory();

    NSString * homePath = [homeDictionartstringByAppendingPathComponent:@"atany.archiver"];

    BOOL isFinish = [NSKeyedArchiverarchiveRootObject:fastArraytoFile:homePath];

    if (isFinish) {

        NSLog(@"归档成功");

    }

    /**

     获取数据

     */

    NSArray * NetWorkArray = [NSKeyedUnarchiverunarchiveObjectWithFile:homePath];

    NSLog(@"%@",NetWorkArray);



5、多个对象的归档

    /**

     先获取一个文件路径

     */

    NSString * filePath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES).lastObject;

    filePath = [NSStringstringWithFormat:@"%@/%@",filePath,@"filePathSources.txt"];

    /**

     输出路径

     */

    NSLog(@"%@",filePath);

    /**

     创建要存储的数据

     */

    NSArray * nameArray =@[@"A",@"B"];

    NSDictionary * nameDict =@{@"name":@"A",@"sex":@"man"};

    /**

     创建一个数据缓存区

     */

    NSMutableData * data = [NSMutableDatadata];

    /**

     将数据归档

     */

    NSKeyedArchiver * archiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];

    /**

     开始归档

     */

    [archiver encodeObject:nameArrayforKey:@"nameArray"];

    [archiver encodeObject:nameDictforKey:@"nameDict"];

    /**

     完成归档

     */

    [archiver finishEncoding];

    /**

     将数据写入文件

     */

    BOOL isFinsish = [datawriteToFile:filePath atomically:NO];

    if (isFinsish) {

        NSLog(@"成功归档");

    }

    

    /**

     解归档,恢复数据

     */

    NSData * unEncodeData = [NSDatadataWithContentsOfFile:filePath];

    NSKeyedUnarchiver * unarcher = [[NSKeyedUnarchiveralloc]initForReadingWithData:unEncodeData];

    NSArray * NetWorkArray = [unarcherdecodeObjectForKey:@"nameArray"];

    [unarcher finishDecoding];

    NSLog(@"%@",NetWorkArray);

6、对某一个类的归档

创建的类

#import <Foundation/Foundation.h>


@interface Person :NSObject <NSCoding>

@property(copy,nonatomic)NSString * nameStr;

@property(assign,nonatomic)NSInteger age;

@property(copy,nonatomic)NSString * addressStr;

@end




#import "Person.h"


@implementation Person


#pragma mark 归档方法的实现

-(void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:self.nameStrforKey:@"Kname"];

    [aCoder encodeInteger:_ageforKey:@"Kage"];

    [aCoder encodeObject:_addressStrforKey:@"Kaddress"];

}


#pragma mark 解归档

-(id)initWithCoder:(NSCoder *)aDecoder{

    if (self = [superinit]) {

        _nameStr = [aDecoderdecodeObjectForKey:@"Kname"];

        _addressStr = [aDecoderdecodeObjectForKey:@"Kaddress"];

        _age = [aDecoderdecodeIntegerForKey:@"Kage"];

    }

    returnself;

}


@end



进行类的归档实例

    /**

     归档类的数据初始化

     */

    Person * pe = [[Personalloc]init];

    pe.nameStr =@"NetWork小贱";

    pe.age =110;

    pe.addressStr =@"北京朝阳双桥";

    

    /**

     创建归档的路径

     */

    NSString * path =NSHomeDirectory();

    NSString * filePath = [pathstringByAppendingPathComponent:@"t.txt"];

    /**

     开始归档

     */

    NSMutableData * data  = [NSMutableDatadata];

    NSKeyedArchiver * archiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];

    [archiver encodeObject:peforKey:@"person"];

    [archiver finishEncoding];

    /**

     将数据写入文件

     */

    BOOL isWr = [datawriteToFile:filePath atomically:YES];

    if (isWr) {

        NSLog(@"归档成功");

    }

    

    

    /**

     解归档

     */

    NSData * UnArchiverData = [NSDatadataWithContentsOfFile:filePath];

    NSKeyedUnarchiver * UnArchiver = [[NSKeyedUnarchiveralloc]initForReadingWithData:UnArchiverData];

    /**

     创建类型对象

     */

    Person * NetWorkPerson = [UnArchiverdecodeObjectForKey:@"person"];

    NSLog(@"%@---%ld---%@",NetWorkPerson.nameStr,NetWorkPerson.age,NetWorkPerson.addressStr);