3

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

zumzum
  • 17,984
  • 26
  • 111
  • 172
  • It is compiled and works fine here (Xcode 13.2), probably you have some naming conflict with other code. – Asperi Apr 01 '22 at 05:46
  • this is weird. I can't see any naming conflicts as of now. I am running on Xcode Version 13.3 (13E113). – zumzum Apr 01 '22 at 15:25
  • Retested with Xcode 13.3.1 / iOS 15.4 - compiled & works fine. Which deployment target version do you have? – Asperi May 24 '22 at 05:10

2 Answers2

8

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

lorem ipsum
  • 21,175
  • 5
  • 24
  • 48
  • Or declare the `body` method like this: `func body(content: SwiftUI.Content) -> some View` – rob mayoff May 24 '22 at 13:22
  • @robmayoff `Content` is usually a generic `` or a `typealias` it isn't a part of `SwiftUI` so `SwiftUI.Content` will not work. `ViewModifier.Content` will likely work but why compensate every time you need to use it (or others) when you can just rename the duplicate. It seems better for continuity too. – lorem ipsum May 24 '22 at 13:24
  • Oops yes I should have said `ViewModifier.Content`, not `SwiftUI.Content`. – rob mayoff May 24 '22 at 13:34
0

SwiftUI errors are really weird. In this case, the error is with the Color constructor. Change Color(UIColor.opaqueSeparator)

to

Color(uiColor: UIColor.opaqueSeparator)

TheLivingForce
  • 532
  • 9
  • 21