I am trying to add a Codable model to a realm DB. The model works and values are passed from an API call how ever, I want to create a Array in my Realm DB so when I hit a save button, the Model is added to DB.
how ever I get this error for my create
Cannot convert value of type 'Data?' to expected argument type 'Object'
func create<T: Object>(_ objects: [T]) where T: Codable {
do {
let placesData = try? JSONEncoder().encode(objects)
try realm.write {
realm.add(placesData)
}
} catch {
}
}
saving in UserDefaults is like this
private func putModelArray<T>(_ value: [T]?, forKey key: String) where T: Codable {
guard let value = value else {
storage.removeObject(forKey: key)
return
}
let placesData = try? JSONEncoder().encode(value)
storage.setValue(placesData, forKey: key)
}