I am trying to utilize the UIImage array in SWiftUI class for forEach loop but Xcode always gives me the error ‘Failed to produce diagnostic for expression;’. I also tried using the Data array but same error. Below is my code. It will be great if someone can point what I am doing wrong?
struct Sample: View {
let imageArray: [UIImage]
var body: some View {
ForEach(imageArray) { image in
Image(uiImage: image)
}
}
}
3
Answers
I was able to fix the problem by adding id in ForEach loop. Below is how my code looks now.
What I learned from here is, for objects such as UIImage, if you need to enumerate, you must add id property on the ForEach loop. To solve the problem, I created a model class containing id and image property and enumerated it.
The ForEach operator requires elements of the array to conform to identifiable. For something like UIImage, you can just pass self.
Personally, I would also wrap it in a Stack, just to be explicit
this works for me: