-1

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)
    }
King
  • 1,885
  • 3
  • 27
  • 84

1 Answers1

0

This

realm.add(placesData)

takes an object argument while you send Data to it

if you get data from json and for it you use Decodable that doesn't mean you have to use Encodable

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87