我目前正在开发一个 Swift 应用程序,它通过他们的 Mobile Buy SDK 与 Shopify API 进行交互。 .我在将购物车保存到设备时遇到问题。其他一切正常,但是当我使用以下代码将购物车保存到设备时:
func saveCart() {
NSUserDefaults.standardUserDefaults().setObject(cart, forKey: "cart")
println("cart saved")
}
我收到以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object <BUYCart: 0x7fb5e30512f0> for key cart'
来自 NSUserDefaults
类引用:
The NSUserDefaults
class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData
, NSString
, NSNumber
, NSDate
, NSArray
, or NSDictionary
. If you want to store any other type of object, you should typically archive it to create an instance of NSData
. For more details, see Preferences and Settings Programming Guide.
希望这对您有所帮助!