Swift Compiler Error: "Type 'Watermark' does not conform to protocol 'ViewModifier'"

Viewed 1226

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

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))
    }
}  
Related