saving data array causes crash swift

情到浓时终转凉″ 提交于 2019-12-25 01:21:18

问题


I tried adding an item to an arrar and saving in Userdefault but the app crashed and I would love anyone to point me to what I am doing wrong

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)
    }

error from crash

[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'


回答1:


GMSAutocompletePrediction doesn't conform to NSCoding so you can't save it to user defaults , you may extract important details from it and make a custom model to save



来源:https://stackoverflow.com/questions/58117112/saving-data-array-causes-crash-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!