Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 查找与OptionSet值关联的名称_Swift_Optionsettype - Fatal编程技术网

Swift 查找与OptionSet值关联的名称

Swift 查找与OptionSet值关联的名称,swift,optionsettype,Swift,Optionsettype,所以我有一个选项: struct Ability: OptionSet { let rawValue: Int static let create = Ability(rawValue: 1 << 0) static let read = Ability(rawValue: 1 << 1) static let update = Ability(rawValue: 1 << 2) static let del

所以我有一个
选项

struct Ability: OptionSet {
    let rawValue: Int

    static let create  = Ability(rawValue: 1 << 0)
    static let read    = Ability(rawValue: 1 << 1)
    static let update  = Ability(rawValue: 1 << 2)
    static let delete  = Ability(rawValue: 1 << 3)

    init(rawValue: Int) {
        self.rawValue = rawValue
    }
}
其中
convertoptionString(Ability.read)
将返回
“read”

当然我知道,
OptionSet
s也可以保存多个值,但我已经知道如何处理这种情况,所以这不是我需要帮助的问题


我希望这个问题可以通过某种聪明的思考来解决,但我还没有找到解决方法。有人想试一试吗?

更新您的
struct
以符合
CustomStringConvertible
并实现
description
属性:

struct Ability: OptionSet, CustomStringConvertible {
    let rawValue: Int

    static let create  = Ability(rawValue: 1 << 0)
    static let read    = Ability(rawValue: 1 << 1)
    static let update  = Ability(rawValue: 1 << 2)
    static let delete  = Ability(rawValue: 1 << 3)

    init(rawValue: Int) {
        self.rawValue = rawValue
    }

    var description: String {
        var vals = [String]()
        if self.contains(.create) {
            vals.append("create")
        }
        if self.contains(.read) {
            vals.append("read")
        }
        if self.contains(.update) {
            vals.append("update")
        }
        if self.contains(.delete) {
            vals.append("delete")
        }

        return vals.joined(separator: ",")
    }
}

print(Ability.read)
let opts: Ability = [ .read, .delete ]
print(opts)
struct-Ability:OptionSet,CustomStringConvertible{
让rawValue:Int

static let create=Ability(rawValue:1这很好。有没有办法只扩展
OptionSet
?@亲爱的,当然,你可以让
Ability
保持原样,然后创建
扩展Ability:CustomStringConvertible{
并将
description
放在扩展名中。我指的是
OptionSet
上的扩展名,而不是
Ability
@亲爱的,我不这么认为。从
OptionSet
无法知道给定的
OptionSet
的可能值列表及其对应的名称。这就是为什么我希望我们能做到这一点我不想强制我的代码的使用者去写一个描述变量,因为这样写太单调了。
struct Ability: OptionSet, CustomStringConvertible {
    let rawValue: Int

    static let create  = Ability(rawValue: 1 << 0)
    static let read    = Ability(rawValue: 1 << 1)
    static let update  = Ability(rawValue: 1 << 2)
    static let delete  = Ability(rawValue: 1 << 3)

    init(rawValue: Int) {
        self.rawValue = rawValue
    }

    var description: String {
        var vals = [String]()
        if self.contains(.create) {
            vals.append("create")
        }
        if self.contains(.read) {
            vals.append("read")
        }
        if self.contains(.update) {
            vals.append("update")
        }
        if self.contains(.delete) {
            vals.append("delete")
        }

        return vals.joined(separator: ",")
    }
}

print(Ability.read)
let opts: Ability = [ .read, .delete ]
print(opts)