Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How To Map Two Arrays [Swift]

I have an array named BonusCardsTest of type [BonusCard] that is conformed to Identifiable that has an id and an url property of type String.

var bonusCardsTest: [BonusCard] = []

struct BonusCard: Identifiable {
    var id = UUID().uuidString
    var url: String
}

I also have an array named getBonusURLsArray of type [String] that contains urls.

What I want is to assign each element of getBonusURLsArray to the url property of bonusCardsTest.
For example, if getBonusURLsArray has two elements – "https://test1.com", "https://test2.com", I want the BonusCard array to look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var bonusCardsTest: [BonusCard] = [
BonusCard(url: "https:test1.com"),
BonusCard(url: "https:test2.com"),
]

How do I do that?

>Solution :

As Larme says, you could map your array of URLs to BonusCards:

let bonusCards = getBonusURLsArray.map { BonusCard(url: $0 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading