Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSCoder.h NSGeometry.h NSKeyedArchiver.h |
Related sample code |
The NSCoder
abstract class declares the interface used by concrete subclasses to transfer objects and other Objective-C data items 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 for Cocoa.
– encodeArrayOfObjCType:count:at:
– encodeBool:forKey:
– encodeBycopyObject:
– encodeByrefObject:
– encodeBytes:length:
– encodeBytes:length:forKey:
– encodeConditionalObject:
– encodeConditionalObject:forKey:
– encodeDataObject:
– encodeDouble:forKey:
– encodeFloat:forKey:
– encodeInt:forKey:
– encodeInteger:forKey:
– encodeInt32:forKey:
– encodeInt64:forKey:
– encodeObject:
– encodeObject:forKey:
– encodePoint:
– encodePoint:forKey:
– encodePropertyList:
– encodeRect:
– encodeRect:forKey:
– encodeRootObject:
– encodeSize:
– encodeSize:forKey:
– encodeValueOfObjCType:at:
– encodeValuesOfObjCTypes:
– encodeNXObject:
Deprecated in Mac OS X v10.5
– decodeArrayOfObjCType:count:at:
– decodeBoolForKey:
– decodeBytesForKey:returnedLength:
– decodeBytesWithReturnedLength:
– decodeDataObject
– decodeDoubleForKey:
– decodeFloatForKey:
– decodeIntForKey:
– decodeIntegerForKey:
– decodeInt32ForKey:
– decodeInt64ForKey:
– decodeObject
– decodeObjectForKey:
– decodePoint
– decodePointForKey:
– decodePropertyList
– decodeRect
– decodeRectForKey:
– decodeSize
– decodeSizeForKey:
– decodeValueOfObjCType:at:
– decodeValuesOfObjCTypes:
– decodeNXObject
Deprecated in Mac OS X v10.5
Returns a Boolean value that indicates whether the receiver supports keyed coding of objects.
- (BOOL)allowsKeyedCoding
The default implementation returns NO
. Concrete subclasses that support keyed coding, such as NSKeyedArchiver
, need to override this method to return YES
.
NSCoder.h
Returns a Boolean value that indicates whether an encoded value is available for a string.
- (BOOL)containsValueForKey:(NSString *)key
The string is passed as key. Subclasses must override this method if they perform keyed coding.
NSCoder.h
Decodes an array of count items, whose Objective-C type is given by itemType.
- (void)decodeArrayOfObjCType:(const char *)itemType count:(NSUInteger)count at:(void *)address
The items are decoded into the buffer beginning at address, which must be large enough to contain them all. itemType must contain exactly one type code. NSCoder
’s implementation invokes decodeValueOfObjCType:at:
to decode the entire array of items. If you use this method to decode an array of Objective-C objects, you are responsible for releasing each object.
This method matches an encodeArrayOfObjCType:count:at:
message used during encoding.
For information on creating an Objective-C type code suitable for itemType, see the “Type Encodings” section in the “The Objective-C Runtime System” chapter of The Objective-C 2.0 Programming Language.
NSCoder.h
Decodes and returns a boolean value that was previously encoded with encodeBool:forKey:
and associated with the string key.
- (BOOL)decodeBoolForKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Decodes a buffer of data that was previously encoded with encodeBytes:length:forKey:
and associated with the string key.
- (const uint8_t *)decodeBytesForKey:(NSString *)key returnedLength:(NSUInteger *)lengthp
The buffer’s length is returned by reference in lengthp. The returned bytes are immutable. Subclasses must override this method if they perform keyed coding.
NSCoder.h
Decodes a buffer of data whose types are unspecified.
- (void *)decodeBytesWithReturnedLength:(NSUInteger *)numBytes
NSCoder
’s implementation invokes decodeValueOfObjCType:at:
to decode the data as a series of bytes, which this method then places into a buffer and returns. The buffer’s length is returned by reference in numBytes. If you need the bytes beyond the scope of the current autorelease pool, you must copy them.
This method matches an encodeBytes:length:
message used during encoding.
NSCoder.h
Decodes and returns an NSData object that was previously encoded with encodeDataObject:
. Subclasses must override this method.
- (NSData *)decodeDataObject
The implementation of your overriding method must match the implementation of your encodeDataObject:
method. For example, a typical encodeDataObject:
method encodes the number of bytes of data followed by the bytes themselves. Your override of this method must read the number of bytes, create an NSData object of the appropriate size, and decode the bytes into the new NSData object. Your overriding method should return an autoreleased NSData object.
NSCoder.h
Decodes and returns a double value that was previously encoded with either encodeFloat:forKey:
or encodeDouble:forKey:
and associated with the string key.
- (double)decodeDoubleForKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Decodes and returns a float value that was previously encoded with encodeFloat:forKey:
or encodeDouble:forKey:
and associated with the string key.
- (float)decodeFloatForKey:(NSString *)key
If the value was encoded as a double, the extra precision is lost. Also, if the encoded real value does not fit into a float, the method raises an NSRangeException
. Subclasses must override this method if they perform keyed coding.
NSCoder.h
Decodes and returns a 32-bit integer value that was previously encoded with encodeInt:forKey:
, encodeInteger:forKey:
, encodeInt32:forKey:
, or encodeInt64:forKey:
and associated with the string key.
- (int32_t)decodeInt32ForKey:(NSString *)key
If the encoded integer does not fit into a 32-bit integer, the method raises an NSRangeException
. Subclasses must override this method if they perform keyed coding.
NSCoder.h
Decodes and returns a 64-bit integer value that was previously encoded with encodeInt:forKey:
, encodeInteger:forKey:
, encodeInt32:forKey:
, or encodeInt64:forKey:
and associated with the string key.
- (int64_t)decodeInt64ForKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Decodes and returns an NSInteger value that was previously encoded with encodeInt:forKey:
, encodeInteger:forKey:
, encodeInt32:forKey:
, or encodeInt64:forKey:
and associated with the string key.
- (NSInteger)decodeIntegerForKey:(NSString *)key
If the encoded integer does not fit into the NSInteger size, the method raises an NSRangeException
. Subclasses must override this method if they perform keyed coding.
NSCoder.h
Decodes and returns an int value that was previously encoded with encodeInt:forKey:
, encodeInteger:forKey:
, encodeInt32:forKey:
, or encodeInt64:forKey:
and associated with the string key.
- (int)decodeIntForKey:(NSString *)key
If the encoded integer does not fit into the default integer size, the method raises an NSRangeException
. Subclasses must override this method if they perform keyed coding.
NSCoder.h
Decodes an Objective-C object that was previously encoded with any of the encode...Object:
methods.
- (id)decodeObject
NSCoder
’s implementation invokes decodeValueOfObjCType:at:
to decode the object data.
Subclasses may need to override this method if they override any of the corresponding encode...Object:
methods. For example, if an object was encoded conditionally using the encodeConditionalObject:
method, this method needs to check whether the object had actually been encoded.
The implementation for the concrete subclass NSUnarchiver
returns an object that is retained by the unarchiver and is released when the unarchiver is deallocated. Therefore, you must retain the returned object before releasing the unarchiver. NSKeyedUnarchiver
’s implementation, however, returns an autoreleased object, so its life is the same as the current autorelease pool instead of the keyed unarchiver.
NSCoder.h
Decodes and returns an autoreleased Objective-C object that was previously encoded with encodeObject:forKey:
or encodeConditionalObject:forKey:
and associated with the string key.
- (id)decodeObjectForKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Decodes and returns an NSPoint structure that was previously encoded with encodePoint:
.
- (NSPoint)decodePoint
NSGeometry.h
Decodes and returns an NSPoint structure that was previously encoded with encodePoint:forKey:
.
- (NSPoint)decodePointForKey:(NSString *)key
NSKeyedArchiver.h
Decodes a property list that was previously encoded with encodePropertyList:
.
- (id)decodePropertyList
NSCoder.h
Decodes and returns an NSRect structure that was previously encoded with encodeRect:
.
- (NSRect)decodeRect
NSGeometry.h
Decodes and returns an NSRect structure that was previously encoded with encodeRect:forKey:
.
- (NSRect)decodeRectForKey:(NSString *)key
NSKeyedArchiver.h
Decodes and returns an NSSize structure that was previously encoded with encodeSize:
.
- (NSSize)decodeSize
NSGeometry.h
Decodes and returns an NSSize structure that was previously encoded with encodeSize:forKey:
.
- (NSSize)decodeSizeForKey:(NSString *)key
NSKeyedArchiver.h
Decodes a single value, whose Objective-C type is given by valueType.
- (void)decodeValueOfObjCType:(const char *)valueType at:(void *)data
valueType must contain exactly one type code, and the buffer specified by data must be large enough to hold the value corresponding to that type code. For information on creating an Objective-C type code suitable for valueType, see the “Type Encodings” section in “The Objective-C Runtime System” chapter of The Objective-C 2.0 Programming Language.
Subclasses must override this method and provide an implementation to decode the value. In your overriding implementation, decode the value into the buffer beginning at data. If your overriding method is capable of decoding an Objective-C object, your method must also retain that object. Clients of this method are then responsible for releasing the object.
This method matches an encodeValueOfObjCType:at:
message used during encoding.
NSCoder.h
Decodes a series of potentially different Objective-C types.
- (void)decodeValuesOfObjCTypes:(const char *)valueTypes, ...
valueTypes is a single string containing any number of type codes. The variable arguments to this method consist of one or more pointer arguments, each of which specifies the buffer in which to place a single decoded value. For each type code in valueTypes, you must specify a corresponding pointer argument whose buffer is large enough to hold the decoded value. If you use this method to decode Objective-C objects, you are responsible for releasing them.
This method matches an encodeValuesOfObjCTypes:
message used during encoding.
NSCoder
’s implementation invokes decodeValueOfObjCType:at:
to decode individual types. Subclasses that implement the decodeValueOfObjCType:at:
method do not need to override this method.
For information on creating Objective-C type codes suitable for valueTypes, see the “Type Encodings” section in “The Objective-C Runtime System” chapter of The Objective-C 2.0 Programming Language.
NSCoder.h
Encodes an array of count items, whose Objective-C type is given by itemType.
- (void)encodeArrayOfObjCType:(const char *)itemType count:(NSUInteger)count at:(const void *)address
The values are encoded from the buffer beginning at address. itemType must contain exactly one type code. NSCoder
’s implementation invokes encodeValueOfObjCType:at:
to encode the entire array of items. Subclasses that implement the encodeValueOfObjCType:at:
method do not need to override this method.
This method must be matched by a subsequent decodeArrayOfObjCType:count:at:
message.
For information on creating an Objective-C type code suitable for itemType, see the “Type Encodings” section in “The Objective-C Runtime System” chapter of The Objective-C Programming Language.
NSCoder.h
Encodes boolv and associates it with the string key.
- (void)encodeBool:(BOOL)boolv forKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Can be overridden by subclasses to encode object so that a copy, rather than a proxy, is created upon decoding.
- (void)encodeBycopyObject:(id)object
NSCoder
’s implementation simply invokes encodeObject:
.
This method must be matched by a corresponding decodeObject
message.
NSCoder.h
Can be overridden by subclasses to encode object so that a proxy, rather than a copy, is created upon decoding.
- (void)encodeByrefObject:(id)object
NSCoder
’s implementation simply invokes encodeObject:
.
This method must be matched by a corresponding decodeObject
message.
NSCoder.h
Encodes a buffer of data whose types are unspecified.
- (void)encodeBytes:(const void *)address length:(NSUInteger)numBytes
The buffer to be encoded begins at address, and its length in bytes is given by numBytes.
This method must be matched by a corresponding decodeBytesWithReturnedLength:
message.
NSCoder.h
Encodes a buffer of data, bytesp, whose length is specified by lenv, and associates it with the string key.
- (void)encodeBytes:(const uint8_t *)bytesp length:(NSUInteger)lenv forKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Can be overridden by subclasses to conditionally encode object, preserving common references to that object.
- (void)encodeConditionalObject:(id)object
In the overriding method, object should be encoded only if it’s unconditionally encoded elsewhere (with any other encode...Object:
method).
This method must be matched by a subsequent decodeObject
message. Upon decoding, if object was never encoded unconditionally, decodeObject
returns nil
in place of object. However, if object was encoded unconditionally, all references to object must be resolved.
NSCoder
’s implementation simply invokes encodeObject:
.
NSCoder.h
Conditionally encodes a reference to objv and associates it with the string key only if objv has been unconditionally encoded with encodeObject:forKey:
.
- (void)encodeConditionalObject:(id)objv forKey:(NSString *)key
Subclasses must override this method if they support keyed coding.
The encoded object is decoded with the decodeObjectForKey:
method. If objv was never encoded unconditionally, decodeObjectForKey:
returns nil
in place of objv.
NSCoder.h
Encodes a given NSData
object.
- (void)encodeDataObject:(NSData *)data
Subclasses must override this method.
This method must be matched by a subsequent decodeDataObject
message.
NSCoder.h
Encodes realv and associates it with the string key.
- (void)encodeDouble:(double)realv forKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Encodes realv and associates it with the string key.
- (void)encodeFloat:(float)realv forKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Encodes the 32-bit integer intv and associates it with the string key.
- (void)encodeInt32:(int32_t)intv forKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Encodes the 64-bit integer intv and associates it with the string key.
- (void)encodeInt64:(int64_t)intv forKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Encodes intv and associates it with the string key.
- (void)encodeInt:(int)intv forKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Encodes a given NSInteger
and associates it with a given key.
- (void)encodeInteger:(NSInteger)intv forKey:(NSString *)key
Subclasses must override this method if they perform keyed coding.
NSCoder.h
Encodes object.
- (void)encodeObject:(id)object
NSCoder
’s implementation simply invokes encodeValueOfObjCType:at:
to encode object. Subclasses can override this method to encode a reference to object instead of object itself. For example, NSArchiver
detects duplicate objects and encodes a reference to the original object rather than encode the same object twice.
This method must be matched by a subsequent decodeObject
message.
NSCoder.h
Encodes the object objv and associates it with the string key.
- (void)encodeObject:(id)objv forKey:(NSString *)key
Subclasses must override this method to identify multiple encodings of objv and encode a reference to objv instead. For example, NSKeyedArchiver
detects duplicate objects and encodes a reference to the original object rather than encode the same object twice.
NSCoder.h
Encodes point.
- (void)encodePoint:(NSPoint)point
NSCoder
’s implementation invokes encodeValueOfObjCType:at:
to encode point.
This method must be matched by a subsequent decodePoint
message.
NSGeometry.h
Encodes point and associates it with the string key.
- (void)encodePoint:(NSPoint)point forKey:(NSString *)key
NSKeyedArchiver.h
Encodes the property list aPropertyList.
- (void)encodePropertyList:(id)aPropertyList
NSCoder
’s implementation invokes encodeValueOfObjCType:at:
to encode aPropertyList.
This method must be matched by a subsequent decodePropertyList
message.
NSCoder.h
Encodes rect.
- (void)encodeRect:(NSRect)rect
NSCoder
’s implementation invokes encodeValueOfObjCType:at:
to encode rect.
This method must be matched by a subsequent decodeRect
message.
NSGeometry.h
Encodes rect and associates it with the string key.
- (void)encodeRect:(NSRect)rect forKey:(NSString *)key
NSKeyedArchiver.h
Can be overridden by subclasses to encode an interconnected group of Objective-C objects, starting with rootObject.
- (void)encodeRootObject:(id)rootObject
NSCoder
’s implementation simply invokes encodeObject:
.
This method must be matched by a subsequent decodeObject
message.
NSCoder.h
Encodes size.
- (void)encodeSize:(NSSize)size
NSCoder
’s implementation invokes encodeValueOfObjCType:at:
to encode size.
This method must be matched by a subsequent decodeSize
message.
NSGeometry.h
Encodes size and associates it with the string key.
- (void)encodeSize:(NSSize)size forKey:(NSString *)key
NSKeyedArchiver.h
Must be overridden by subclasses to encode a single value residing at address, whose Objective-C type is given by valueType.
- (void)encodeValueOfObjCType:(const char *)valueType at:(const void *)address
valueType must contain exactly one type code.
This method must be matched by a subsequent decodeValueOfObjCType:at:
message.
For information on creating an Objective-C type code suitable for valueType, see the “Type Encodings” section in “The Objective-C Runtime System” chapter of The Objective-C 2.0 Programming Language.
NSCoder.h
Encodes a series of values of potentially differing Objective-C types.
- (void)encodeValuesOfObjCTypes:(const char *)valueTypes, ...
valueTypes is a single string containing any number of type codes. The variable arguments to this method consist of one or more pointer arguments, each of which specifies a buffer containing the value to be encoded. For each type code in valueTypes, you must specify a corresponding pointer argument.
This method must be matched by a subsequent decodeValuesOfObjCTypes:
message.
NSCoder
’s implementation invokes encodeValueOfObjCType:at:
to encode individual types. Subclasses that implement the encodeValueOfObjCType:at:
method do not need to override this method. However, subclasses that provide a more efficient approach for encoding a series of values may override this method to implement that approach.
For information on creating Objective-C type codes suitable for valueTypes, see the “Type Encodings” section in “The Objective-C Runtime System” chapter of The Objective-C 2.0 Programming Language.
NSCoder.h
Returns the memory zone used to allocate decoded objects.
- (NSZone *)objectZone
NSCoder
’s implementation simply returns the default memory zone, as given by NSDefaultMallocZone()
.
Subclasses must override this method and the setObjectZone:
method to allow objects to be decoded into a zone other than the default zone. In its overriding implementation of this method, your subclass should return the current memory zone (if one has been set) or the default zone (if no other zone has been set).
NSCoder.h
NSCoder
’s implementation of this method does nothing.
- (void)setObjectZone:(NSZone *)zone
Can be overridden by subclasses to set the memory zone used to allocate decoded objects.
Subclasses must override this method and objectZone
to allow objects to be decoded into a zone other than the default zone. In its overriding implementation of this method, your subclass should store a reference to the current memory zone.
NSCoder.h
During encoding, this method should return the system version currently in effect.
- (unsigned)systemVersion
During decoding, this method should return the version that was in effect when the data was encoded.
By default, this method returns the current system version, which is appropriate for encoding but not for decoding. Subclasses that implement decoding must override this method to return the system version of the data being decoded.
NSCoder.h
Returns the version in effect for the class with a given name.
- (NSInteger)versionForClassName:(NSString *)className
The version in effect for the class named className or NSNotFound
if no class named className exists.
When encoding, this method returns the current version number of the class. When decoding, this method returns the version number of the class being decoded. Subclasses must override this method.
The version number applies to NSArchiver
/NSUnarchiver
, but not to NSKeyedArchiver
/NSKeyedUnarchiver
. A keyed archiver does not encode class version numbers.
+ setVersion:
(NSObject
)+ version
(NSObject
)NSCoder.h
© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-23)