简体   繁体   English

协议类型'*'的值不能符合'*'; 只有结构/枚举/类类型可以符合协议

[英]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.通过使applyStyle非泛型可以轻松解决您的问题,因为Style可以用作具体类型。

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:由于 Preset 成员样式是 Style,而不是任何具体类型,因此您不需要 applyStyle 是通用的,您可以简单地说:

  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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 协议类型“XXX”的值不能符合“可识别”; 只有结构/枚举/类类型可以符合协议 - Swift Value of protocol type 'XXX' cannot conform to 'Identifiable'; only struct/enum/class types can conform to protocols 协议类型“Encodable”的值不能符合“Encodable”; 只有结构/枚举/类类型可以符合协议 - Value of protocol type 'Encodable' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols 协议类型“Any”的值不能符合“Equatable”; 只有结构/枚举/类类型可以符合协议 - Value of protocol type 'Any' cannot conform to 'Equatable'; only struct/enum/class types can conform to protocols 类型“MovieSearchContainer.Type”不能符合“Decodable”; 只有结构/枚举/类类型可以符合协议 - Type 'MovieSearchContainer.Type' cannot conform to 'Decodable'; only struct/enum/class types can conform to protocols 使用 if 语句时:类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - While using an if statement: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 请帮忙:类型'()'不能符合'View'; 只有结构/枚举/类类型可以符合协议 - Please help: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 如何修复类型“()”不能符合“视图”; 只有结构/枚举/类类型可以符合协议 - how do i fix Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols Swift - 类型“(字符串,JSON)”不能符合“字符串协议”; 只有结构/枚举/类类型可以符合协议 - Swift - Type '(String, JSON)' cannot conform to 'StringProtocol'; only struct/enum/class types can conform to protocols type() 不能符合 View; 只有结构/枚举/类类型可以符合协议 - Type () cannot conform to View; only struct/enum/class types can conform to protocols 类型 &#39;() -&gt; ()&#39; 不能符合 &#39;View&#39;; 只有结构/枚举/类类型可以符合协议 - Type '() -> ()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM