bbaars / SwiftUI-Sound-Visualizer

117 stars 10 forks source link

ForEach "soundSamples" logs console errors #2

Open robnotyou opened 2 years ago

robnotyou commented 2 years ago

In Xcode 14, I get repeated console errors:

ForEach<Array<Float>, Float, BarView>: the ID -47.630486 occurs multiple times within the collection, this will give undefined results!

This is caused by an issue with the ForEach...

ForEach(mic.soundSamples, id: \.self) { level in
    BarView(value: self.normalizeSoundLevel(level: CGFloat(level)))
}

...because soundSamples are not Identifiable (since each level can occur many times)

robnotyou commented 2 years ago

I was able to fix this issue by using:

ForEach(mic.soundSamples.indices, id: \.self) { i in
    BarView(value: self.normalizeSoundLevel(level: CGFloat(mic.soundSamples[i])))
}