In NSCoder, you can call encodeObject: and encodeObject:forkey:. And this for many data types. What is the difference beetween those calls? How to use them? Why isn’t there a encodeDataObject:forkey: or encodePropertyList:forKey:?
In NSCoder, you can call encodeObject: and encodeObject:forkey: . And this for many data
Share
Keyed vs. Unkeyed Accessors
Most of the time you just call the
encodeSomething:forKey:method and supply a key that you later use to get the value back from a decoder:The unkeyed version of the call is older, I guess that serialization used to work differently back before 10.2.
Specialized Object Accessors
There isn’t a separate
encodeDataObject:call sinceNSDataconforms toNSCoding, so that you can encode it using regularencodeObject:. The same applies to property lists – a property list is just a dictionary, and a dictionary is a regular object that can be encoded usingencodeObject:.What maybe got you confused is the number of specialized methods for encoding primitive types like
BOOLorNSUInteger. These have to do with the type system. When encoding and decoding objects, the interface can use theidtype and it works regardless of the particular object type:There is no such general, “wildcard” type for primitive types, therefore the number of specialized getters and setters:
Theoretically you could use
void*and cast, but that’s clumsy and the encoding interface wouldn’t know the size of the object to encode anyway.