Extending NSCoding

NSCoding is used often through many iOS, macOS, watchOS and tvOS apps, and usually results in instances that conform being archived into data for transportation or persisting.

So lets extend NSCoding so we can easily archive that object. It’s very simple but very useful:

Now any type that conforms to Archivable will have to conform to NSCoding and will also be easily archived by calling instance.archivedData. Because we’ve extended the protocol itself, all types that declare they conform get this behaviour for free. Neat ?