Xcode报错:Attempt to insert non-property list object null for key lastProcessed
Xcode报错:Attempt to insert non-property list object null for key lastProcessed

Xcode报错:Attempt to insert non-property list object null for key lastProcessed

在Swift中,如果尝试将 UserDefaults 中的值改为nil,Xcode会输出:

Attempt to set a non-property-list object <null> as an NSUserDefaults/CFPreferences value for key lastProcessed
FAULT: NSInvalidArgumentException: Attempt to insert non-property list object null for key lastProcessed; (user info absent)

因为 UserDefaults 不能直接存储 nil。

解决方案

1、使用特殊值表示 nil:

// 将时间改为 0,而不是改为 nil
appStorage.firstUsed = Date(timeIntervalSince1970: 0)

2、删除 UserDefaults 的key:

UserDefaults.standard.removeObject(forKey: "lastProcessed")

   

如果您认为这篇文章给您带来了帮助,您可以在此通过支付宝或者微信打赏网站开发者。

欢迎加入我们的 微信交流群QQ交流群,交流更多精彩内容!
微信交流群二维码 QQ交流群二维码

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注