归档类与解归档类的属性以及方法的了解

时间:2022-02-15 16:21:37
归档的方式就是将数据通过密文的方式存储,或者说是可以通过二进制进行存储。

归档的属性和方法:NSKeyedArchiver

 FOUNDATION_EXPORT NSString * const NSInvalidArchiveOperationException;
FOUNDATION_EXPORT NSString * const NSInvalidUnarchiveOperationException;
上面的两个常量都是用来处理异常的,一个是归档一个是解压归档的时候使用的。s

FOUNDATION_EXPORT NSString * const NSKeyedArchiveRootObjectKey NS_AVAILABLE(10_9, 7_0);
//这个关键字就是不管是类的归档方法还是相应的个别的实力发那个是,都是可以用这个关键字来充当对象的关键字的key。archivedRootDataWithObject:、unarchiveObjectWithData等

+ (NSData *)archivedDataWithRootObject:(id)rootObject; //将对象归档为Data类型的数据
+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;// rootObject对象写在path制定的路径下面
- (instancetype)initForWritingWithMutableData:(NSMutableData *)data; //


@property NSPropertyListFormat outputFormat; //NSPropertyListFormat这个是一种枚举类型
typedef NS_ENUM(NSUInteger, NSPropertyListFormat) {
NSPropertyListOpenStepFormat = kCFPropertyListOpenStepFormat,//明文的方式
NSPropertyListXMLFormat_v1_0 = kCFPropertyListXMLFormat_v1_0,//这个是xml的格式
NSPropertyListBinaryFormat_v1_0 = kCFPropertyListBinaryFormat_v1_0//这个是二进制的格式
};

- (void)finishEncoding;
//完成编码(归档)

+ (void)setClassName:(NSString *)codedName forClass:(Class)cls;
- (void)setClassName:(NSString *)codedName forClass:(Class)cls;
//在编码过程中,编码器首先检查编码器的自己的表,如果没有可以设置相应映射给该类的。
//设置归档的类名字

+ (NSString *)classNameForClass:(Class)cls;
- (NSString *)classNameForClass:(Class)cls;
//获取归档的类名字,获取该类中的映射名字

- (void)encodeObject:(id)objv forKey:(NSString *)key;
- (void)encodeConditionalObject:(id)objv forKey:(NSString *)key;
- (void)encodeBool:(BOOL)boolv forKey:(NSString *)key;
- (void)encodeInt:(int)intv forKey:(NSString *)key; // native int
- (void)encodeInt32:(int32_t)intv forKey:(NSString *)key;
- (void)encodeInt64:(int64_t)intv forKey:(NSString *)key;
- (void)encodeFloat:(float)realv forKey:(NSString *)key;
- (void)encodeDouble:(double)realv forKey:(NSString *)key;
- (void)encodeBytes:(const uint8_t *)bytesp length:(NSUInteger)lenv forKey:(NSString *)key;
//为相应的存储(归档)类进行的相应的值设置,也就是key的方式进行设置,我们这里一般都是通过关键字和字符串的方式进行设置。
eg:person类中:[person encodeObject:name forKey:@"name"]; //这样就表示相应的值进行设置
]

// 设置是否安全编码(归档),编码是归档对数据的一种方式,或者说编码的方式是多种的,而归档又是数据存储的一种方式。
- (void)setRequiresSecureCoding:(BOOL)b ;
//***不需要通过设置安全编码归档到解归档的的过程(也就是这个过程不是一个连贯的过程)
//安全归档只不过是一种确保所有的类都是安全归档,这个方法为yes的时候,如果一个类没有安全归档将会跑出异常。注意:getter方法是在父类NSCoder,可以查看它了解更加多的安全编码。

@interface NSKeyedArchiver : NSCoder {
@private
void *_stream;
NSUInteger _flags;
id _delegate;
id _containers;
id _objects;
id _objRefMap;
id _replacementMap;
id _classNameMap;
id _conditionals;
id _classes;
NSUInteger _genericKey;
void *_cache;
NSUInteger _cacheSize;
NSUInteger _estimatedCount;
void *_reserved2;
id _visited;
void * __strong _reserved0;
}//私有属性


代理方法:

@protocol NSKeyedArchiverDelegate <NSObject>
@optional
//替代
- (id)archiver:(NSKeyedArchiver *)archiver willEncodeObject:(id)object;

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

// 通知
- (void)archiver:(NSKeyedArchiver *)archiver willReplaceObject:(id)object withObject:(id)newObject;

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

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

分类方法(object的):
@interface NSObject (NSKeyedArchiverObjectSubstitution)

@property (readonly) Class classForKeyedArchiver;

- (id)replacementObjectForKeyedArchiver:(NSKeyedArchiver *)archiver;

+ (NSArray *)classFallbacksForKeyedArchiver;

@end

//编码方式:编码的两个方法
@protocol NSCoding(必须实现的方法)

- (void)encodeWithCoder:(NSCoder *)aCoder;//编码(保存)
- (id)initWithCoder:(NSCoder *)aDecoder; //解码(获取)
@end


下面的是解归档的属性和方法 NSKeyedUnarchiver

@interface NSKeyedUnarchiver : NSCoder {
@private
id _delegate;
uint32_t _flags;
id _objRefMap;
id _replacementMap;
id _nameClassMap;
id _tmpRefObjMap;
id _refObjMap;
int32_t _genericKey;
id _data;
void *_offsetData;
id _containers;
id _objects;
const uint8_t *_bytes;
uint64_t _len;
id _helper;
void * __strong _reserved0;
}//私有属性,基本和归档的类是一样的。

解归档的方式:
+ (id)unarchiveObjectWithData:(NSData *)data;
+ (id)unarchiveObjectWithFile:(NSString *)path;
- (instancetype)initForReadingWithData:(NSData *)data;

- (void)finishDecoding;

+ (void)setClass:(Class)cls forClassName:(NSString *)codedName;
- (void)setClass:(Class)cls forClassName:(NSString *)codedName;
+ (Class)classForClassName:(NSString *)codedName;
- (Class)classForClassName:(NSString *)codedName;

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

- (id)decodeObjectForKey:(NSString *)key;
- (BOOL)decodeBoolForKey:(NSString *)key;
- (int)decodeIntForKey:(NSString *)key; // may raise a range exception
- (int32_t)decodeInt32ForKey:(NSString *)key;
- (int64_t)decodeInt64ForKey:(NSString *)key;
- (float)decodeFloatForKey:(NSString *)key;
- (double)decodeDoubleForKey:(NSString *)key;
- (const uint8_t *)decodeBytesForKey:(NSString *)key returnedLength:(NSUInteger *)lengthp NS_RETURNS_INNER_POINTER;

- (void)setRequiresSecureCoding:(BOOL)b NS_AVAILABLE(10_8, 6_0);

//上面的方法基本和归档类中的方法是一样,只不过是一个相反的过程。



@protocol NSKeyedUnarchiverDelegate <NSObject>
@optional

// error handling
- (Class)unarchiver:(NSKeyedUnarchiver *)unarchiver cannotDecodeObjectOfClassName:(NSString *)name originalClasses:(NSArray *)classNames;

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

// notification
- (void)unarchiver:(NSKeyedUnarchiver *)unarchiver willReplaceObject:(id)object withObject:(id)newObject;
ded objects.

- (void)unarchiverWillFinish:(NSKeyedUnarchiver *)unarchiver;
// Notifies the delegate that decoding is about to finish.

- (void)unarchiverDidFinish:(NSKeyedUnarchiver *)unarchiver;
// Notifies the delegate that decoding has finished.

@end


系统定义好的分类方法:
@interface NSObject (NSKeyedUnarchiverObjectSubstitution)

+ (Class)classForKeyedUnarchiver;

@end


总结:

1、数据的归档和解归档是对数据的存数和获取的过程

2、归档和解归档都是继承于编码类NSCoder。都是在数据归档存储过程中所需要使用到的方法类类和方法。