Can't figure out why nothing conforms to "View"

I cannot figure out how to fix this error:

Static method 'buildExpression' requires that 'ToolbarItemGroup<TupleView<(Button<Label<Text, Image>>, EditButton)>>' conform to 'View'

This is my code:

var body: some View {
        NavigationView {
            List {
                ForEach(items) { item in
                    NavigationLink {
                        Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))")
                    } label: {
                        Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))
                    }
                }
                .onDelete(perform: deleteItems)
            }
            .navigationBarTitleDisplayMode(.automatic)
            .toolbar {
                ToolbarItemGroup(placement: .topBarTrailing) {
                    Button(action: addItem) {
                        Label("Add Item", systemImage: "plus")
                    }
                    EditButton()
                }
                ToolbarItem(placement: .principal) {
                    Label("hi")
                }
            }
        }
    }

Im using the Xcode 15 beta 2 to build for iOS 17

Answered by BabyJ in 757594022

Xcode/Swift like sending you in different directions with errors messages, but this one I'm sure is easier to solve. The issue is not with the ToolbarItemGroup but with the Label in the ToolbarItem.

ToolbarItem(placement: .principal) {
    Label("hi") // error is here
}

Label requires two arguments and you have only passed it one. Either change it to a Text view or provide the second parameter, either systemImage: or image:.

Accepted Answer

Xcode/Swift like sending you in different directions with errors messages, but this one I'm sure is easier to solve. The issue is not with the ToolbarItemGroup but with the Label in the ToolbarItem.

ToolbarItem(placement: .principal) {
    Label("hi") // error is here
}

Label requires two arguments and you have only passed it one. Either change it to a Text view or provide the second parameter, either systemImage: or image:.

I would start by commenting out the call to EditButton() and see if that fixes the error.

If that doesn't work, comment out the code to create the "hi" toolbar item.

The only other thing I see that could cause a problem is the following line of code:

Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))")

That string looks fishy. It looks like you're missing a parenthesis at the end of item.timeStamp. I would start with something like the following:

Text("Item at \(item.timestamp)")

And add the format argument to the item text after you get the basic text to appear in the list.

On Xcode 14.2 (may be different on 15).

I had to make some changes to make it compile:

ToolbarItemGroup(placement: .topBarTrailing)
// replaced by
ToolbarItemGroup(placement: .navigationBarTrailing) {  // Other placement possible, but not topBarTrailing
Label("hi")
// replaced by
Text("hi")

Here is the simple code to test:

    var body: some View {
        NavigationView {
            List {
                ForEach(0..<3) { item in
                    Text("Hello")
                }
            }
                .navigationBarTitleDisplayMode(.automatic)
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        Button(action: { print("Added")}) {
                            Label("Add Item", systemImage: "plus")
                        }
                        EditButton()
                    }
                    ToolbarItem(placement: .principal) {
                        Text("hi")
                    }
                }
            }
    }

Can't figure out why nothing conforms to "View"
 
 
Q