保存数据数组导致崩溃 swift

saving data array causes crash swift

我尝试将一个项目添加到数组并保存在 Userdefault 中,但应用程序崩溃了,我希望任何人指出我做错了什么

private func putArray(_ value: GMSAutocompletePrediction?, forKey key: String) {
        guard let value = value else {
            return
        }
        log("THE MESSAGE \(value)", .fuck)
        var newArray = getArray(forKey: key)
        log("THE MESSAGE ARRAY \(newArray)", .fuck)
        if newArray.contains(value) {
            newArray.remove(at: newArray.firstIndex(of: value)!)
        } else {
            newArray.append(value)
        }

        storage.setValue(NSKeyedArchiver.archivedData(withRootObject: newArray), forKey: key)
    }

崩溃错误

[GMSAutocompletePrediction encodeWithCoder:]: unrecognized selector sent to instance 0x2818f9ce0 2019-09-26 13:40:07.300856+0100 MAX.NG Staging Debug[4440:1410011] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GMSAutocompletePrediction encodeWithCoder:]: unrecognized selector sent to instance 0x2818f9ce0'

GMSAutocompletePrediction 不符合 NSCoding 所以你不能将它保存到用户默认值,你可以从中提取重要的细节并制作自定义模型来保存