I have a problem with converting Realm type List < Int> array to Int array [Int] when writing jsonData to Realm.
struct Movies: Codable {
let genreIDs: [Int]?
}
class CachedMovies: Object {
let genreIDs: List<Int>?
}
func saveMovies(movies:[Movies]) {
do {
let realm = try! Realm()
try realm.write {
movies.forEach { (movie) in
let movieRealm = CachedMovies()
movieRealm.id = movie.id ?? 0
movieRealm.title = movie.title ?? ""
movieRealm.genreIDs = movie.genreIDs ?? [] // here's error: Cannot assign value of type '[Int]?' to type 'List<Int>?'
realm.add(movieRealm, update: .modified)
}
}
}
I tried this but array comes empty
var newArr = Array(movieRealm.genreIDs)
newArr = movie.genreIDs ?? []
I also tried to change List type to Results and convert Results to Int by using this extension but it's not working.
extension Results {
func toArray<T>(type: T.Type) -> [T] {
return compactMap { $0 as ? T }
}
}
Could you please help me how can I assign arrays correctly?