shasha 2018年07月03日 程序员 598 0



The NSCoder abstract class declares the interface used by concrete subclasses to transfer objects and other values between memory and some other format. This capability provides the basis for archiving (where objects and data items are stored on disk) and distribution (where objects and data items are copied between different processes or threads). The concrete subclasses provided by Foundation for these purposes are NSArchiver, NSUnarchiver, NSKeyedArchiver, NSKeyedUnarchiver, and NSPortCoder. Concrete subclasses of NSCoder are referred to in general as coder classes, and instances of these classes as coder objects (or simply coders). A coder object that can only encode values is referred to as an encoder object, and one that can only decode values as a decoder object.

NSCoder operates on objects, scalars, C arrays, structures, and strings, and on pointers to these types. It does not handle types whose implementation varies across platforms, such as union, void *, function pointers, and long chains of pointers. A coder object stores object type information along with the data, so an object decoded from a stream of bytes is normally of the same class as the object that was originally encoded into the stream. An object can change its class when encoded, however; this is described in Archives and Serializations Programming Guide.
The AV Foundation framework adds methods to the NSCoder class to make it easier to create archives including Core Media time structures, and extract Core Media time structure from archives.



       NSCoder可以操作对象、标量、C数组、结构体和字符串,还有这些类型的指针。它不能操作的类型是那些跨平台执行的变量,例如:union、void *、函数指针和长链表的指针。




//*> 该协议声明在NSObject.h中 
@protocol NSCoding 
//*> 序列化数据、编码成NSCoder对象 
- (void)encodeWithCoder:(NSCoder *)aCoder; 
//*> 反序列化数据,解码NSCoder对象 
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;  


//*> Animal.h 
@interface Animal : NSObject<NSCoding> 
@property (nonatomic, strong) NSString * name; 
@property (nonatomic, assign) unsigned int sex; 
@property (nonatomic, assign) unsigned int age; 
@property (nonatomic, strong) NSString * location; 
//*> Animal.m 
#define kA_name     @"name" 
#define kA_sex      @"sex" 
#define kA_age      @"age" 
#define kA_location @"location" 
- (instancetype)initWithCoder:(NSCoder *)aDecoder 
    self = [super init]; 
    if (self) 
    {     = [aDecoder decodeObjectForKey:kA_name];      = [aDecoder decodeInt32ForKey:kA_sex]; 
        self.age      = [aDecoder decodeInt32ForKey:kA_age]; 
        self.location = [aDecoder decodeObjectForKey:kA_location]; 
    return self; 
- (void)encodeWithCoder:(NSCoder *)aCoder 
    [aCoder encodeObject:_name forKey:kA_name]; 
    [aCoder encodeInt32:_sex forKey:kA_sex]; 
    [aCoder encodeInt32:_age forKey:kA_age]; 
    [aCoder encodeObject:_location forKey:kA_location]; 



initWithCode:和其他init方法一样,在对对象执行操作之前,需要使用超类对它们进行初始化。为此,可以采用两种方式,具体取决于父类,如果父类采用了NSCoding协议,则应该调用[super initWithCoder:decoder];否则,只需要调用[super init]即可。NSObject 不采用NSCoding协议,因此我们可以使用简单的init方法




微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!