Objective-C-如何使用NSInteger初始化枚举?

Fel*_*ges 3 objective-c

我正在开发应用程序,当我尝试使用编码具有enum属性的模型时,我的问题就开始了NSCoding。所以我有一个想法将其转换为rawValue和返回的方式。我四处看看,然后到了宏NS_ENUM,所以我的代码如下所示:

typedef NS_ENUM(NSInteger, SectionType) {
    SectionTypeText = 0,
    SectionTypeVideo = 1,
    SectionTypeLink = 2,
    SectionTypeFile = 3,
    SectionTypeQuiz = 4,
    SectionTypeAudio = 5,
    SectionTypeGame = 6,
    SectionTypeHomework = 7
};
Run Code Online (Sandbox Code Playgroud)

但是我找不到将它们转换为关联值以及返回的方法。我该怎么办?有没有比NS_ENUM宏更好的方法?

Dep*_*o B 5

我的Objective-C有点生锈,但我想我会抛弃它:

   SectionType type = (SectionType) 2;
Run Code Online (Sandbox Code Playgroud)

背面的工作原理相同:

   int typeNumber = type;
Run Code Online (Sandbox Code Playgroud)

  • 是的,它是可自由转换的,您甚至不需要投放它 (3认同)