简体   繁体   English

结构不符合RawRepresentable协议?

[英]Struct does not conform to RawRepresentable protocol?

I have a struct here, which generates errors when Xcode tries to compile it 我这里有一个结构,当Xcode尝试编译它时会生成错误

public struct GATToIPPermissions : OptionSet {

    public init(rawValue: UInt)


    public static var read: GATToIPPermissions { get {}}

    public static var write: GATToIPPermissions { get {}}

    public static var event: GATToIPPermissions { get {}}

    public static var all: GATToIPPermissions { get {}}
}

The error I get is Type GATToIPPermissions does not conform to protocol RawRepresentable . 我得到的错误是Type GATToIPPermissions does not conform to protocol RawRepresentable However, I dont get any indication as to why it doesn't conform. 但是,我没有得到任何关于为什么它不符合要求的指示。

Can any of you spot the problem? 你们中有人可以发现问题吗?

That syntax you're written is what you would use within a protocol . 您编写的语法就是在protocol使用的语法。 If it were in a protocol, it would declare "Conforming types must implement an initializer called init(rawValue:) , and have getters for the following properties of type GATToIPPermissions : read , write , event , and all " 如果在协议中,它将声明“符合类型必须实现一个称为init(rawValue:)的初始化程序,并且必须具有GATToIPPermissions类型的以下属性的GATToIPPermissionsreadwriteeventall

But you're not aiming to write declarations in a protocol , you're looking to write implementations in a struct , and here is how that would look: 但是,您的目的不是要在protocol编写声明,而是要在struct编写实现,这是这样的:

public struct GATToIPPermissions : OptionSet {

    public init(rawValue: UInt) {
        //initialize self with `rawValue`
    }


    public static let read = GATToIPPermissions() //set me to the right value
    public static let write = GATToIPPermissions() //set me to the right value
    public static let event = GATToIPPermissions() //set me to the right value
    public static let all = GATToIPPermissions() //set me to the right value
}

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

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