2015-12-08 24 views
1

我有这个结构定义在自己的文件,并希望在别处和测试中使用它。如何使此OptionSetType结构公开?

struct UserPermissions : OptionSetType { 
    let rawValue: UInt 
    static let CreateFullAccount = UserPermissions(rawValue: 1 << 1) 
    static let CreateCustomAccount = UserPermissions(rawValue: 1 << 2) 
} 

当我尝试使用它时,我得到一个关于如何由于该类型使用内部类型而无法声明属性的错误。

public var userPermissions = UserPermissions() 

所以我想我可以公开它,但是这给了我一个关于需要公共init函数的错误。

public struct UserPermissions : OptionSetType { 
    public let rawValue: UInt 
    static let CreateFullAccount = UserPermissions(rawValue: 1 << 1) 
    static let CreateCustomAccount = UserPermissions(rawValue: 1 << 2) 
} 

所以我想补充这该结构的定义,它导致编译器将崩溃:

public init(rawValue: Self.RawValue) { 
    super.init(rawValue) 
} 

一些访问控制的东西我还在周围包裹我的头斯威夫特。我究竟做错了什么?我怎样才能使用这个OptionSetType?

回答

2

如果您访问了OptionSetType protocol reference页面,您会找到您需要的示例。你的UserPermissions是一个结构体,没有super被调用。

现在回答你的问题:

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]