I am trying to open a sheet when tapping on an item. I followed this questions Sheet inside ForEach doesn't loop over items SwiftUI answer. I get this Error: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
, I don't understand what is causing it. I tried multiple solutions and they all lead to the same Error.
@State var selectedSong: Int? = nil
@State var songList: [AlbumSong] = []
VStack {
ForEach(songList.enumerated().reversed(), id: \.offset) { index, song in
HStack {
Text("\(index + 1).").padding(.leading, 8)
VStack {
Text(song.title)
Text(song.artist)
}
}.onTapGesture {
self.selectedSong = index
}
}
}
}
.sheet(item: self.$selectedSong) { selectedMovie in
SongPickerEdit(songList: $songList, songIndex: selectedMovie)
I also tried setting songIndex
to being an AlbumSong
and then implemented this sheet:
.sheet(item: self.$selectedSong) {
SongPickerEdit(songList: $songList, songIndex: self.songList[$0])
}
struct SongPickerEdit: View {
@Binding var songList: [AlbumSong]
@State var songIndex: Int?
var body: some View {
}
}
struct AlbumSong: Identifiable, Codable {
@DocumentID var id: String?
let title: String
let duration: TimeInterval
var image: String
let artist: String
let track: String
}