【问题标题】:How to use NSCoding protocol with an enum?如何使用带有枚举的 NSCoding 协议?
【发布时间】:2013-01-15 09:05:29
【问题描述】:

我有这个枚举:

typedef types {
    HBIntineraryTypeVisited = 0,
    HBIntineraryTypeUnvisited,
    HBIntineraryTypeUnknown,
    HBIntineraryTypeDeleted,
} HBIntineraryType;

并希望使用 nscoding 协议将其与其他一些变量一起存储

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
       _name = [aDecoder decodeObjectForKey:@"name"];
       // todo decode enum object
    }
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:_name forKey:@"name"];
    // todo encode enum object
}

我使用什么编码器方法来解码和编码这种枚举?

【问题讨论】:

  • decodeInt/encodeInt(有一些演员表)。

标签: ios objective-c enums nscoding


【解决方案1】:

一般而言,enums 的表示可能会有所不同。使用 Objective-C 时,您应该使用 the NS_ENUM macro 来确定使用哪种类型来表示枚举。 this article还有更多背景。

【讨论】:

  • 这是将链接作为答案的缺点。你永远无法判断他们什么时候会开始显示 404 :(
  • Big Nerd Ranch 的大部分博客文章已被移动。这是新网址:bignerdranch.com/blog/enum-num-num
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多