结构不符合原始可表示协议?



我这里有一个结构,当 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.但是,我没有得到任何迹象表明为什么它不符合。

你们中有人能发现问题吗?

你写的语法就是你在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
}

最新更新