[英]How can I make this OptionSetType struct public?
I have this struct defined in its own file and want to use it elsewhere and in testing. 我在自己的文件中定义了此结构,并希望在其他地方和测试中使用它。
struct UserPermissions : OptionSetType {
let rawValue: UInt
static let CreateFullAccount = UserPermissions(rawValue: 1 << 1)
static let CreateCustomAccount = UserPermissions(rawValue: 1 << 2)
}
When I try to use it I get an error about how the property cannot be declared public because the type uses an internal type. 当我尝试使用它时,由于该类型使用内部类型,因此出现关于如何无法将该属性声明为公共的错误。
public var userPermissions = UserPermissions()
So I thought I could make it public, but that gives me an error about needing a public init function. 因此,我以为可以将其公开,但是这给了我关于需要公共init函数的错误。
public struct UserPermissions : OptionSetType {
public let rawValue: UInt
static let CreateFullAccount = UserPermissions(rawValue: 1 << 1)
static let CreateCustomAccount = UserPermissions(rawValue: 1 << 2)
}
So I add this to the definition of the struct, which causes the compiler to crash: 因此,我将其添加到结构的定义中,这将导致编译器崩溃:
public init(rawValue: Self.RawValue) {
super.init(rawValue)
}
Some of the access control stuff I'm still wrapping my head around in Swift. 一些访问控制的东西,我仍在用Swift包扎。 What am I doing wrong?
我究竟做错了什么? How can I use this OptionSetType?
如何使用此OptionSetType?
Had you visited the OptionSetType
protocol reference page, you would have found an example of what you need. 如果您访问了
OptionSetType
协议参考页,那么您将找到所需的示例。 Your UserPermissions
is a struct, there's no super
to be called. 您的
UserPermissions
是一个结构,没有super
可以调用。
Now to answer your question: 现在回答您的问题:
public struct UserPermissions : OptionSetType {
public let rawValue: UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
static let CreateFullAccount = UserPermissions(rawValue: 1 << 1)
static let CreateCustomAccount = UserPermissions(rawValue: 1 << 2)
}
// Usage:
let permissions: UserPermissions = [.CreateFullAccount, .CreateCustomAccount]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.