Hello! I am new to SwiftUI. Recently I am trying to create a list view for my app. However, when I add the list view using ForEach, an error will show up saying "Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project". When I comment out the ForEach, everything back to normal. I have no idea what is wrong to the code. Hope someone can help me out! Thank you!
The code throw error:
The error shows on line 2. If I comment out line 12-15, everything back to normal.
The definition of Playlist and Song is
The code throw error:
Code Block Swift struct PlaylistDetailView: View { var playlist: Playlist var body: some View { NavigationView{ VStack{ if playlist.songs.count == 0 { Text("No Songs") } else{ Text("Have Songs") List{ ForEach(playlist.songs){ item in SongRow(song: item) } } } } .navigationTitle(playlist.name) .navigationBarTitleDisplayMode(.inline) .toolbar(content: { ToolbarItem(placement: .navigationBarLeading) { Button(action: {}, label: { Text("Edit") }) } ToolbarItem(placement: .navigationBarTrailing) { Button(action: {}, label: { Image(systemName: "plus") }) } }) } } }
The error shows on line 2. If I comment out line 12-15, everything back to normal.
The definition of Playlist and Song is
Code Block Swift struct Playlist: Codable, Identifiable { var id = UUID() var name: String var songs: [Song] } struct Song: Codable { var Name: String var URLs: URL }
In your code, Playlist is Identifiable, but the Element type of playlist.songs is not.
Try adding this extension to Song:
(Or else, you can add id: parameter to ForEach.)
Swift compiler cannot find the right initializer of ForEach when the Element is not Identifiable, but at least it should show some sort of errors on line 12.
You can send a bug report to Apple using the Apple's Feedback Assistant, or you can choose bugs.swift.org as suggested.
Try adding this extension to Song:
Code Block extension Song: Identifiable { var id: String {Name} //<- Please change this, if you know something better for `id` }
(Or else, you can add id: parameter to ForEach.)
Swift compiler cannot find the right initializer of ForEach when the Element is not Identifiable, but at least it should show some sort of errors on line 12.
You can send a bug report to Apple using the Apple's Feedback Assistant, or you can choose bugs.swift.org as suggested.