在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")
