归档类与解归档类的属性以及⽅法的了解
归档的⽅式就是将数据通过密⽂的⽅式存储,或者说是可以通过⼆进制进⾏存储。
归档的属性和⽅法: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:、unarchiv
+ (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 _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的):
object to@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 _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。都是在数据归档存储过程中所需要使⽤到的⽅法类类和⽅法。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论