Error:Ambiguous use of 'FamilyMembers'in line 25

Replies

You may be new to the forum, so you don't know yet the good practices.

  • You should close the threads you opened by marking the answer as correct if it solves your issue.
  • post the code if it is short enough directly, and format it with code formatter, not through an attached file

For your question: do we need to count ourselves ti find which is line 25 ?

I checked in Xcode, it is this one:

ForEach(FamilyMembers, id: \.name){ (FamilyMember) in

Reason is that you use the same name FamilyMembers for the struct and for the instance.

  • so it is ambiguous
  • and instances should start with lowercase.

So change the code as follows:

struct List_View: View {
    struct FamilyMembers: Hashable {
        let name: String
        let thing: String
    }

    @State private var familyMembers = [    // <<-- lowerCase
        FamilyMembers(name: "Dad", thing: "It is my Dad, he set's many rules that is annoying." ),
        FamilyMembers(name: "Mum", thing: "It is my Mum, I love her,she always kiss me." ),
        FamilyMembers(name: "Brother", thing: "It is my little brother, he is cute but naughty." ),
        FamilyMembers(name: "grandpa", thing: "It is my grandfather, He often asks IQ qestions for me to answer." ),
        FamilyMembers(name: "grandma", thing: "It is my grandmother, she is fun but serious." )
    ]
    
    var body: some View {
        NavigationView {
            
            List{
                ForEach(familyMembers, id: \.name){ familyMember in    // <<-- lowerCase
                    NavigationLink(destination:
                        VStack {
                            Text(familyMember.thing)   // <<-- lowerCase and $familyMember, not $familyMembers
                    }
                    ){
                        HStack{
                            Text(familyMember.name)   // <<-- lowerCase and $familyMember, not $familyMembers
                        }
                    }
                }
            }
        }
        .navigationTitle("My Family Members")
    }
}
  • Note that Swift avoids using underscore: ListView is better than List_View
  • for a better readability of your code, struct should be named FamilyMember and not FamilyMembers.

And don't forget to close the thread on the correct answer if that works now. Otherwise, explain what the remaining problem is.

Thanks