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?
正如 Larme 所说,您可以将您的 URL 数组映射到BonusCard
:
let bonusCards = getBonusURLsArray.map { BonusCard(url: $0) }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.