本文介绍了NSCoder和自定义类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用NSCoder编码和解码自定义类型?

How do you use NSCoder to encode and decode custom types?

例如,如何将NSCoder与实例"STATE"一起使用,

For example, how would you use NSCoder with an instance of "STATE" where:

typedef enum { ON, OFF } STATE;

推荐答案

您可以将它们视为整数,因为它们是隐式分配的整数值:

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"];
}

这篇关于NSCoder和自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 15:40