Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C - how to init enum with NSInteger?

Tags:

objective-c

I'm working on an application and my problem started when i tried to encode a model that has an enum property using NSCoding. So i had the idea to convert it to the rawValue and the way back. I looked around a bit and came around the macro NS_ENUM, so my code looks like this:

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

But i could find no possible way to convert these to the associated value and the way back. How could i do it? There is a better approach than the NS_ENUM macro?

like image 541
Felipe Borges Avatar asked May 08 '18 18:05

Felipe Borges


1 Answers

My Objective-C is a bit rusty, but I think I would just cast it:

   SectionType type = (SectionType) 2;

Back works the same:

   int typeNumber = type;
like image 168
Lucas van Dongen Avatar answered Oct 06 '22 20:10

Lucas van Dongen