IOS的归档小总结
Posted iOS的美丽岁月
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS的归档小总结相关的知识,希望对你有一定的参考价值。
1、什么是归档?
归档最通用的定义是存储有组织的数据。归档的目的是长时间存放有组织的数据集,确保其将来能够被精细地检索。
2、归档方法和属性的介绍
/**
实例化一个归档对象的方法
1、init
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 = [homeDictionart stringByAppendingPathComponent:@"atany.archiver"];
BOOL isFinish = [NSKeyedArchiver archiveRootObject:fastArray toFile:homePath];
if (isFinish)
NSLog(@"归档成功");
/**
获取数据
*/
NSArray * NetWorkArray = [NSKeyedUnarchiver unarchiveObjectWithFile:homePath];
NSLog(@"%@",NetWorkArray);
5、多个对象的归档
/**
先获取一个文件路径
*/
NSString * filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
filePath = [NSString stringWithFormat:@"%@/%@",filePath,@"filePathSources.txt"];
/**
输出路径
*/
NSLog(@"%@",filePath);
/**
创建要存储的数据
*/
NSArray * nameArray = @[@"A",@"B"];
NSDictionary * nameDict = @@"name":@"A",@"sex":@"man";
/**
创建一个数据缓存区
*/
NSMutableData * data = [NSMutableData data];
/**
将数据归档
*/
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
/**
开始归档
*/
[archiver encodeObject:nameArray forKey:@"nameArray"];
[archiver encodeObject:nameDict forKey:@"nameDict"];
/**
完成归档
*/
[archiver finishEncoding];
/**
将数据写入文件
*/
BOOL isFinsish = [data writeToFile:filePath atomically:NO];
if (isFinsish)
NSLog(@"成功归档");
/**
解归档,恢复数据
*/
NSData * unEncodeData = [NSData dataWithContentsOfFile:filePath];
NSKeyedUnarchiver * unarcher = [[NSKeyedUnarchiver alloc]initForReadingWithData:unEncodeData];
NSArray * NetWorkArray = [unarcher decodeObjectForKey:@"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.nameStr forKey:@"Kname"];
[aCoder encodeInteger:_age forKey:@"Kage"];
[aCoder encodeObject:_addressStr forKey:@"Kaddress"];
#pragma mark 解归档
-(id)initWithCoder:(NSCoder *)aDecoder
if (self = [super init])
_nameStr = [aDecoder decodeObjectForKey:@"Kname"];
_addressStr = [aDecoder decodeObjectForKey:@"Kaddress"];
_age = [aDecoder decodeIntegerForKey:@"Kage"];
return self;
@end
进行类的归档实例
/**
归档类的数据初始化
*/
Person * pe = [[Person alloc]init];
pe.nameStr = @"NetWork小贱";
pe.age = 110;
pe.addressStr = @"北京朝阳双桥";
/**
创建归档的路径
*/
NSString * path = NSHomeDirectory();
NSString * filePath = [path stringByAppendingPathComponent:@"t.txt"];
/**
开始归档
*/
NSMutableData * data = [NSMutableData data];
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:pe forKey:@"person"];
[archiver finishEncoding];
/**
将数据写入文件
*/
以上是关于IOS的归档小总结的主要内容,如果未能解决你的问题,请参考以下文章