简体   繁体   中英

Value of protocol type '*' cannot conform to '*'; only struct/enum/class types can conform to protocols

I am facing the above error while trying this:

    protocol Style {}
    
    struct StyleA: Style {}
    struct StyleA: Style {}
    struct StyleA: Style {}

    struct Preset: Identifiable {
        let id: UUID = UUID()
        let title: String
        let style: Style
    }

    extension View {
        public func applyStyle<S>(_ style: S) where S : Style {
            // USe the style here
        }
    }
    

    // Initializg the data
    static let mockedData: [Preset] = [
        .init(title: "Title A", style: StyleA()),
        .init(title: "Title A", style: StyleB()),
        .init(title: "Title A", style: StyleC()),
    ]
    
// This line gives the error
    myView.applyStyle(mockedData.first!.style)


How can I fix it? Shouldn't it resolve the concrete type?

Thanks for your help.

You're running into the problem of protocols not conforming to themselves . Your problem can be easily solved by making applyStyle non-generic, since Style can be used as a concrete type.

extension View {
    public func applyStyle(_ style: Style) {
        // USe the style here
    }
}

Since the Preset member style is Style, not any concrete type, you don't need the applyStyle to be generic, you can simply say:

  public func applyStyle(_ style: Style) 

try

public protocol Style {}

struct StyleA: Style {}
struct StyleB: Style {}
struct StyleC: Style {}

struct Preset: Identifiable {
    let id: UUID = UUID()
    let title: String
    let style: Style
}

extension View {
    public func applyStyle(_ style: Style){
        // USe the style here
    }
}


// Initializg the data
let mockedData: [Preset] = [
    .init(title: "Title A", style: StyleA()),
    .init(title: "Title A", style: StyleB()),
    .init(title: "Title A", style: StyleC()),
]

myView.applyStyle(mockedData.first!.style)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM