问题
I have tried to save a SKProduct object with archiving it with the class NSKeyedArchiver but it generates an error as -[SKProduct encodeWithCoder:]: unrecognized selector sent to instance.
+ (void)saveProduct:(SKProduct *)product
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:product];
[[NSUserDefaults standardUserDefaults] setValue:data forKey:PRODUCT];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Is there a way to save an instance of SKProduct or not?
回答1:
Your SKProduct class needs to conform to the NSCoding protocol.
In other words you need to fill out these two methods in your SKProduct class.
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
This will take your SKProduct class and break it down so that it can be serialized and unserialized.
来源:https://stackoverflow.com/questions/14178790/how-to-save-skproduct-object-locally