1

i want to convert Array from 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
    }
}

and the genre is

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

How can i convert from array genre (Struct) to List realm which is GenreEntity ?

1
  • 1
    Please don’t include images or links in your questions. Include code and structures as text so we can use them in answers with copy/paste. See images and links are evil. Images and links are not searchable so they may not be of use to future readers. Commented Mar 28, 2021 at 13:59

1 Answer 1

2

This should just be a matter of adding the new GenreEntity objects to an array and then return the entire array once done.

This should do it

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
}
Sign up to request clarification or add additional context in comments.

8 Comments

@amaulana Glad to help. I also formatted the code in your question to make it readable. There are formatting tools in the editor when asking questions - the brackets {} that format the code can really help keep the code lined up.
@amaulana also a quick trick is to do Ctrl + i. This re-indents you code.
Could you please say how can I convert List<Int> to [Int]? Tried many answers from SO but still can't find solution :(
@Abrcd18 You probably don't want to don't need to do that. Leaving realm objects as realm objects has some benefits over casting them to a swift array. May I ask why you want/need to do that? You can just do this Array(your realm list) but again, that may not be the best choice depending on the use case.
@Abrcd18 It's really hard to diagnose issues within comments. This code Array(your realm list) works, 100% of the time and casts a Realm List to an array. However, there are a LOT of downfalls to doing that. It would be best to create a question with a minimal example of the difficulty and an an explanation of what you're trying to do. Do that, link it here and we'll take a look.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.