Struct不符合RawRepresentable协议?

时间:2017-08-04 16:42:54

标签: swift xcode struct protocols rawrepresentable

我这里有一个结构,当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 {}}
}

我得到的错误是Type GATToIPPermissions does not conform to protocol RawRepresentable。但是,我没有任何迹象表明为什么它不符合。

你们有没有发现这个问题?

1 个答案:

答案 0 :(得分:0)

您编写的语法是您在protocol中使用的语法。如果它在协议中,它将声明“符合类型必须实现名为init(rawValue:)的初始化程序,并且具有GATToIPPermissions类型的以下属性的getter:readwriteeventall

但是你并不打算在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
}