如何将结构数组转换为列表领域?

How to convert Array of Struct to List Realm?

我想将数组从结构转换为列表领域。

static func mapGenreResponsetoGenreEntity( input genre: [GenreModel]) -> List {
    var list = List<GenreEntity>()
    
    return  genre.map { result in
        let newGenre =  GenreEntity()
        newGenre.gamesCount = result.gamesCount ?? 0
        newGenre.id = result.id ?? 0
        newGenre.imageBackground = result.imageBackground ?? "Unknown"
        newGenre.name = result.name ?? "Unknown"
        newGenre.slug = result.slug ?? "Unknown"
  
        list.append(newGenre)
  
        return list
    }
}

类型是

struct GenreModel: Codable {
  let gamesCount : Int?
  let id : Int?
  let imageBackground : String?
  let name : String?
  let slug : String?
}

如何从数组流派 (Struct) 转换为 List 领域,即 GenreEntity?

这应该只是将新的 GenreEntity 对象添加到数组,然后 return 整个数组完成后的问题。

应该这样做

func convertToList(genreArray: [GenreClass]) -> List<GenreEntityRealmModel> {
    let genreEntityList = List<GenreEntityRealmModel>()
    genreArray.forEach { model in
        let genreEntity = GenreEntity()
        genreEntity.gamesCount = model.gamesCount
        genreEntityList.append(genreEntity)
    }
    return genreEntityList
}