I have one for loop where I found index in repeating hence I want to convert into foreach loop.
for (key, _) in details {
let product = product
details[key]?.title = product.first?.name ?? ""
details[key]?.amount = Double(product.first?.prices ?? 0.0)
}
I tried this as below but still here details[key]? is repeating
details.forEach { key, value in
let product = product
details[key]?.title = cartProduct.first?.name ?? ""
details[key]?.amount = Double(product.first?.prices ?? 0.0)
}
May I know, how to convert this into foreach in Swift so that I don’t need to write [key] every-time.
2
Answers
You can do it this way: