0
votes

I am fairly new to SwiftUI but am pulling my hair out trying to display text from two ForEach loops. I am working on a song app that would display lyrics in stanzas. My data is one array Lyrics that holds Verses which is another array. Each verse is a stanza. And each verse has a string array that stores the lyrics for one line.

//Lyrics variable

@Published var lyrics: [Verse]? = [Verse]()

//Verse structure storing each line

struct Verse: Codable, Identifiable {
    let id = UUID()
    let verseContent: [String]
}

The part I am having trouble is in implementation of getting all of the information into Text within my View. One ForEach loop works and I can get the first line of each of my stanzas as follows

//Builds Fine with this code

 ForEach(lyrics) { verse in
                   Text(verse.verseContent[0])
               }

But the problem is when I try and do a nested ForEach to get all of the lines in each stanza with the following.

    return AnyView(
           ForEach(lyrics) { verse in
               ForEach(verse.verseContent { line in 
                 Text(line)
               )
           }
    )

When I try this I get the following error Explicitly specify the generic arguments to fix this issue Generic parameter 'ID' could not be inferred

Referencing initializer 'init(_:content:)' on 'ForEach' requires that 'String' conform to 'Identifiable'

1

1 Answers

0
votes

Solved! It just needed .self for both of the ForEach statements