ios - 将 AnyObject 保存到 NSUserdefaults

标签 ios objective-c swift shopify

<分区>

我目前正在开发一个 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.

希望这对您有所帮助!

关于ios - 将 AnyObject 保存到 NSUserdefaults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32301690/

相关文章:

iphone - 在 PC 上使用 iPhone 摄像头作为网络摄像头

ios - 如何在Swift中从观察对象中提取字符串值

ios - 如何在 Swift 中使用 SKAction 的执行方法

ios - 这是什么?带有 subview Controller 的 ContainerView 的 UITableView?

ios - 在数组中每第 n 个数字添加一组数字 - iPhone

ios - Express.IO 连接不断失败并显示 HTTP 500

ios - 使用 IP 地址连接到服务器时应用程序传输安全策略错误

json - 使用 map 将 JSON 数组解析为矩阵

iOS Swift - 如何匹配另一个字符串为 "like"的字符串?即 "http"就像 "http://"

ios - swift - 警告 : attempt to present on whose view is not in the window hierarchy