I already take a look around and found the closes answer here:
But this is only applicable for custom objects that is user-created. My problem is serializing “SKProduct” which is a derived class that is non-NSCoding compliant. Specifically, the exact error I encountered:
-[SKProduct encodeWithCoder:]: unrecognized selector sent to instance 0x4027160
Does anyone have similar experience?
I’ll preface this answer by saying there’s probably an easier way; but class substitution during archiving and unarchiving is one approach you could take.
During archiving, you have the option of setting a delegate that conforms to the
NSKeyedArchiverDelegateprotocol. All of the methods are optional. The delegate receives a messagearchiver:willEncodeObject:message during encoding. If you wish to substitute a class during archiving, you can create a substitute object and return it. Otherwise just return the original object.In your case, you could create a ‘shadow object’ for
SKProductthat encapsulates whatever properties on the original class you are interested in serializing. Then substitute that class during archiving. During unarchiving, you could reverse the process and returnSKProductFor illustrative purposes, here’s an example. Mind you, I’ve left out the reverse substitution part – but if you read the docs on
NSKeyedUnarchiverDelegateI think it would be clear.This logs: