How do you use NSCoder to encode and decode custom types?
For example, how would you use NSCoder with an instance of "STATE
" where:
typedef enum { ON, OFF } STATE;
NSCoder 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 (storing objects and data on disk) and distribution (copying objects and data items between different processes or threads).
The NSCoding protocol declares the two methods that a class must implement so that instances of that class can be encoded and decoded. This capability provides the basis for archiving (where objects and other structures are stored on disk) and distribution (where objects are copied to different address spaces).
You can treat them as integers as they are implicitly assigned integer values:
- (void) encodeWithCoder: (NSCoder *)coder {
...
[coder encodeInt:type forKey:@"state"];
}
- (id) initWithCoder: (NSCoder *)coder {
...
state = [coder decodeIntForKey:@"state"];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With