Home > Mobile >  Cannot convert value of type 'String' to expected argument type 'Category'
Cannot convert value of type 'String' to expected argument type 'Category'

Time:04-18

This is my first iOS app development project. I'm still learning, everything is new to me. I'me trying here to create three navigation rectangles that navigates to a View from an array. But there are all sorts of error. Here is the Code

// ContentView.swiftui

 struct ContentView: View {
    
    struct Category : Identifiable{
        let id = UUID()
        let title: String
    }
    var categories = [
        Category( title: "Menu 1"),
        Category( title: "Menu 2"),
        Category( title: "Menu 3"),
    ]
    var body: some View {
        NavigationView {
            ScrollView(.horizontal) {
                HStack {
                        ForEach(categories, id:\.title){category in
                            NavigationLink(destination: CategoryView(categories: category.title))
// Here comes the error (Cannot convert value of type 'String' to expected argument type 'Category')
                                {
                                ZStack{
                                    RoundedRectangle(cornerRadius: 20)
                                        .frame(width: 250, height: 120)
                                        .foregroundColor(.accentColor)
                                    .shadow(color: .black.opacity(0.2), radius: 10, x: 10, y: 10)
                                    .shadow(color: .white.opacity(0.7), radius: 10, x: -10, y: -10)
                                    Text(category.title)
                                }.padding(5)
                            }
                    }
                }
                .padding(10)
            }
        }
        .navigationBarTitleDisplayMode(.inline)
    }
}

The next View file is as follows which is also a simple View.

// CategoryView.swift

struct CategoryView: View {
    var categories : Category
    var body: some View {
        NavigationView {
            List{
                ForEach(questions){question in
                
                Text("Question \(question.id)")
                
                }
        }
.navigationBarTitle("Welcome") // Also this title is not visible, I've tried changing color.
            .navigationTitle(categories.title)
    }
}
}

struct CategoryView_Previews: PreviewProvider {
    
    static var previews: some View {
        CategoryView(categories: categories[1])
    }
}

Thanks in advance

CodePudding user response:

You're passing in a string to the initialiser of CategoryView(categories: category.title). Remove the .title at the end and pass in the category CategoryView(categories: category).

As for the navigation title, the modifier needs to go inside the NavigationView and not attached to the NavigationView itself. This is because you can push multiple views onto one NavigationView with different Navigation Bar Titles.

NavigationView {
    List {
        ForEach(questions) { question in
            Text("Question \(question.id)")
        }
    } // end List
    .navigationBarTitle("Welcome")
} // end NavView
  • Related