swiftswiftuiviewmodifier

TextField ViewModifier not conforming to ViewModifier?


I have this code in one of my views:

struct TextFieldClearButton: ViewModifier {

    @Binding var text: String

    func body(content: Content) -> some View {

        HStack {

            content

            if !text.isEmpty {
                Button(
                    action: { self.text = "" },
                    label: {
                        Image(systemName: "delete.left")
                            .foregroundColor(Color(UIColor.opaqueSeparator))
                    }
                )
            }
        }
    }
}

I get two errors:

  1. Type 'TextFieldClearButton' does not conform to protocol 'ViewModifier'
  2. Static method 'buildBlock' requires that 'Content' conform to 'View'

How can I get rid of these errors and make this modifier compile?

enter image description here

It looks like I can't use ViewModifier at all. Adding super simple case errors out too??:

enter image description here


Solution

  • You likely have a struct/class in your project named Content

    If you have Xcode's standard dark theme the "mint"/"greenish" means it is "Project" defined.

    enter image description here

    When you are using Apple's definition it is pinkish/purple like ViewModifier, View, and String in your screenshot.

    enter image description here

    Search for struct Content, class Content, enum Content, etc. In your project, You will find the duplicate and then just change the name of the duplicate.

    It could also be a generic <Content: SomeProtocol> or <Content> or typealias Content

    You can confirm the duplicate by being more specific

    enter image description here