How to convert List<Int> to [Int]?

Hi there! 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() = ?? 0
        movieRealm.genreIDs = movie.genreIDs ?? [] // here's error: Cannot assign value of type '[Int]?' to type 'List<Int>?'

I tried to change List type to Results and convert Results to Int by using this extension but it's not work.

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?

I've never used Realm, but this should work (if List<Int>() gives you an empty list):

movie.genreIDs = movie.genreIDs ?? List<Int>()

Still gives error: Cannot convert value of type 'List' to expected argument type '[Int]?'

StackOverflow may be more appropriate when it comes to specific libraries:


Thank you!

Terms of Service

Privacy Policy

Cookie Policy