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

问题描述 投票:1回答:1

我正在研究一个应用程序,当我尝试使用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
};

但我找不到将这些转换为相关值和返回方式的可能方法。我怎么能这样做?有比NS_ENUM宏更好的方法吗?

objective-c
1个回答
2
投票

我的Objective-C有点生疏,但我想我会抛出它:

   SectionType type = (SectionType) 2;

返回工作原理相同:

   int typeNumber = type;
© www.soinside.com 2019 - 2024. All rights reserved.