How to save SKProduct object locally?

主宰稳场 提交于 2020-01-14 12:48:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!