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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.