Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSCoding protocol with an enum?

i have this enum:

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

and want to store it along with some other variables using the nscoding protocol

- (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
}

What coder method do i use to decode and encode this kind of enum?

like image 661
bogen Avatar asked Jan 31 '13 09:01

bogen


1 Answers

Generally speaking the representation of enums can vary. When working with Objective-C, you should use the NS_ENUM macro to be sure of which type is used to represent the enumeration. There's more background in this article.

like image 87
Jim Avatar answered Oct 20 '22 03:10

Jim