Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCoder and custom types

Tags:

types

nscoder

How do you use NSCoder to encode and decode custom types?

For example, how would you use NSCoder with an instance of "STATE" where:

typedef enum { ON, OFF } STATE;
like image 690
SK9 Avatar asked Oct 27 '10 03:10

SK9


People also ask

What is a NSCoder?

NSCoder declares the interface used by concrete subclasses to transfer objects and other values between memory and some other format. This capability provides the basis for archiving (storing objects and data on disk) and distribution (copying objects and data items between different processes or threads).

What is NSCoding in Swift?

The NSCoding protocol declares the two methods that a class must implement so that instances of that class can be encoded and decoded. This capability provides the basis for archiving (where objects and other structures are stored on disk) and distribution (where objects are copied to different address spaces).


1 Answers

You can treat them as integers as they are implicitly assigned integer values:

- (void) encodeWithCoder: (NSCoder *)coder {
  ...
  [coder encodeInt:type forKey:@"state"];
}

- (id) initWithCoder: (NSCoder *)coder {
  ...
  state = [coder decodeIntForKey:@"state"];
}
like image 105
Ushox Avatar answered Oct 04 '22 21:10

Ushox