Pergunta

What are the real differences between NSCoding and NSCopying on the low level?

Also, are they supposed to be used independently, together or when is it good to use which one?

Foi útil?

Solução

  • NSCopying is closest to what in other languages would be called clone(), that is, it's used for immediately creating a (functional) new object as a carbon copy of an existing object.

  • NSCoding is closest to serialize()/deserialize, that is, it's used for converting objects to a form that can be persistently stored and revived later, possibly when the program is restarted at some other time.

As you can see, they're not very similar in function, and are definitely supposed to be able to be used independently.

Outras dicas

While they both create a copy of the object in question, they do it in very different ways.

A protocol is really just a promise to implement a specific set of functions. In case of NSCopying it's -copyWithZone: and in case of NSCoding it's –initWithCoder: and -encodeWithCoder:.

NSCopying is used to make an in-memory copy of the object - another identical object.

NSCoding is used to create a serialized representation - an NSData object - or creating an object from such a representation.

NSCoding is about serializing objects to disc.

NSCopying is about being copyable (in memory).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top