问题
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;
回答1:
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"];
}
来源:https://stackoverflow.com/questions/4029760/nscoder-and-custom-types