0

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:

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

How do I do that?

4
  • 1
    map() would be the higher level method, else use a basic for loop. Commented Jul 16, 2022 at 19:35
  • Your title asks about "two arrays", but your question only asks about one. Is that a typo? If not, please clarify your question. Commented Jul 16, 2022 at 21:52
  • @Alexander there are two arrays in the question: bonusCardsTest and getBonusURLsArray Commented Jul 22, 2022 at 18:36
  • @Gleb One is just a result of a computation over the other. The two aren't "mapped" together Commented Jul 22, 2022 at 18:44

1 Answer 1

2

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

let bonusCards = getBonusURLsArray.map { BonusCard(url: $0) }
Sign up to request clarification or add additional context in comments.

Comments

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.