gpt4 book ai didi

swift - 保存在 userdefaults 中会使应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 11:43:54 25 4
gpt4 key购买 nike

我是 swift 新手,尝试将数组保存到用户默认值中,但我的应用程序崩溃了。

我正在使用下面的代码

let currentObject = productsArray[clickedPosition] as Product
print("currentObject===\(currentObject)")
var currentCartArray = [Product]()
currentCartArray.append(currentObject)

UserDefaults.standard.set(currentCartArray, forKey: "Wafaa_MyCart")
UserDefaults.standard.synchronize()

下面是我得到的输出

currentObject===Product(Id: Optional(10007), TitleEN: Optional("PDF 1"), 
TitleAR: Optional("PDF 1 AR"), DescriptionEN: nil,
DescriptionAR: nil, AboutEN: Optional("Merchant 1"),
AboutAR: Optional("Merchant 1"), CategoryId: Optional(1),
UnitSold: Optional(0), CategoryEN: Optional("Category 1"),
CategoryAR: Optional("الفئة الأولى"), CountryId: Optional(23),
CountryEN: Optional("Egypt"), CountryAR: Optional("مصر"),
CountryFlag: Optional("http://xxxx.xxxxx.info//Files/CountryPhotos/EG-128.png"),
MerchantId: Optional(1), MerchantNameEN: Optional("Merchant 1"),
MerchantNameAR: Optional("Merchant 1"), Logo: Optional(""),
CurrencyId: Optional(1), CurrencyCode: Optional(414),
CurrencyEN: Optional("Kuwaiti Dinar"), CurrencyAR: Optional("دينار كويتى"),
SymbolEN: Optional("KWD"), SymbolAR: Optional("KWD"),
Discount: Optional("0%"),
FinalPrice: Optional("1.000"), Price: Optional("1.000"),
OverViewAR: Optional("ddddddd"), OverViewEN: Optional("dddd"),
NoUsersRate: Optional(0), IsFavorite: Optional(false),
IsPurchased: Optional(false),
FavoriteIds: nil, Rate: Optional(3),
DemoVideo: Optional("http://player.vimeo.com/video/1111"),
VideoDuration: Optional("06:12:00"), ViewersCount: Optional(7),
Photo: Optional("http://xxxx.xxxx.info//Images/noimageicon.png"),
AddedOnDate: Optional("28/02/2018 01:43 PM"))

此崩溃显示以下错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object (
"Wafaa.Product(Id: Optional(10007), TitleEN: Optional(\"PDF 1\"),
TitleAR: Optional(\"PDF 1 AR\"), DescriptionEN: nil, DescriptionAR: nil,
AboutEN: Optional(\"Merchant 1\"), AboutAR: Optional(\"Merchant 1\"),
CategoryId: Optional(1), UnitSold: Optional(0), CategoryEN: Optional(\"Category 1\"),
CategoryAR: Optional(\"\U0627\U0644\U0641\U0626\U0629 \U0627\U0644\U0623\U0648\U0644\U0649\"),
CountryId: Optional(23), CountryEN: Optional(\"Egypt\"),
CountryAR: Optional(\"\U0645\U0635\U0631\"),
CountryFlag: Optional(\"http://xxx.xxxx.info//Files/CountryPhotos/EG-128.png\"),
MerchantId: Optional(1), MerchantNameEN: Optional(\"Merchant 1\"),
MerchantNameAR: Optional(\"Merchant 1\"), Logo: Optional(\"\"),
CurrencyId: Optional(1), CurrencyCode: Optional(414),
CurrencyEN: Optional(\"Kuwaiti Dinar\"),
CurrencyAR: Optional(\"\U062f\U064a\U0646\U0627\U0631 \U0643\U0648\U064a\U062a\U0649\"),
SymbolEN: Optional(\"KWD\"), SymbolAR: Optional(\"KWD\"),
Discount: Optional(\"0%\"), FinalPrice: Optional(\"1.000\"),
Price: Optional(\"1.000\"), OverViewAR: Optional(\"ddddddd\"),
OverViewEN: Optional(\"dddd\"), NoUsersRate: Optional(0),
IsFavorite: Optional(false), IsPurchased: Optional(false),
FavoriteIds: nil, Rate: Optional(3),
DemoVideo: Optional(\"http://player.vimeo.com/video/xxxxx\"), VideoDuration: Optional(\"06:12:00\"),
ViewersCount: Optional(7), Photo: Optional(\"http://xxxx.xxxx.info//Images/noimageicon.png\"),
AddedOnDate: Optional(\"28/02/2018 01:43 PM\"))"

我记得,在 Objective-C 中,只有当我有 NULL 时才会崩溃,但这里我没有任何 null,为什么会发生崩溃?

知道我哪里出错了吗?

最佳答案

在实际存储到 UserDefaults 之前,您首先必须将对象序列化为 Data 实例。幸运的是,Swift 4 提供了内置工具,可以轻松进行序列化和反序列化(Codable 协议(protocol))。

看看this tutorial其中深入描述了所需的步骤。

关于swift - 保存在 userdefaults 中会使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49051623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com