2

I don't want to force my users to update to iOS 6 and hence I wanted to find a way to use NSCoder with custom types and enumerations.

I have found this article that explains the recently introduced NS_ENUM macro, that basically makes it easier to the runtime library to retrieve the the metadata of the custom enumeration / type.

Is there an alternative way to encode custom enumerations?

I have found this answer, is encoding as INT all I need to do? Enumerations are int but I am not sure if they can change to int32 or int64.

Community
  • 1
  • 1
mm24
  • 9,280
  • 12
  • 75
  • 170

2 Answers2

2

Yes, encode it as an int and you should be fine. You could create a NSNumber with its value and then encode it. That would be the most common case.

rocir
  • 472
  • 1
  • 4
  • 9
1

Just encode the value as NSNUMBER. Use this for any version of iOS.

 // #pragma mark Protocolo NSCoding
- (void)encodeWithCoder:(nonnull NSCoder *)aCoder {
     [aCoder encodeObject:@(self.customValue) forKey:@"customKey"];
  }

- (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder { 
    self = [super init];
    if (self) {
        self.customValue = [aDecoder decodeObjectForKey:@"customKey"];
    }
    return self;
  }
Ariel Antonio Fundora
  • 1,330
  • 15
  • 20