skip to Main Content

I want to add a ViewModifier, as explained in the following tutorial by Paul Hudson (https://www.hackingwithswift.com/books/ios-swiftui/custom-modifiers).
My ViewModifier is:

import SwiftUI

struct Watermark: ViewModifier {
    var text: String

    func body(content: Content) -> some View {
        ZStack(alignment: .bottomTrailing) {
            content
            Text(text)
                .font(.caption)
                .foregroundColor(.white)
                .padding(5)
                .background(Color.black)
        }
    }
}

extension View {
    func watermarked(with text: String) -> some View {
        self.modifier(Watermark(text: text))
    }
}

But I get the following Errors:

enter image description here

I tried to reproduce this in another project, but there it just works as expected. I tried already cleaning the build folder, remove the derived data, restart Xcode, restart the Mac.

Any ideas on how to solve the issue?

2

Answers


  1. I think you have name conflict, ie. there is another entity named Watermark in your project (or visible from other parts), so try to make this modifier unique. Like

    struct WatermarkModifier: ViewModifier {
     // .. other code
    }
    
    extension View {
        func watermarked(with text: String) -> some View {
            self.modifier(WatermarkModifier(text: text))
        }
    }  
    
    Login or Signup to reply.
  2. I experienced this error before and as what @Asperi said it is naming conflict issue. There may have another entity also named Content in the project.

    And images for reference:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search