NSCoding in iPhone

NSCoding

If a class conforms to <NSCoding> then it can can be done.

If a class conforms to <NSCoding> it implements these required methods.

  • -(id)initWithCoder:(NSCoder*)coder;
  • (void)encodeWithCoder:(NSCoder*)coder;


What these two methods are expected to do

The following describes the purpose of these two methods.

initWithCoder
initialises an instance of the class from an NSCode object
encodeWithCoder
writes an existing instance of the class to an NSCoder object

The following is an example:

code.png

Or

NSCoder is an abstractClass that represents a stream of data. They are used in archiving and unarchiving objects. NSCoder objects are usually used in a method being implemented so that the class conforms to the protocol. (which has something like encodeObject and decodeObject methods in them).

nscoder.jpg


Similar Articles