How to display OptionSet values ​​in a human-readable form?

Swift is of type OptionSet, which basically adds set operations to C-Style bits. Apple uses them quite widely in its framework. Examples include parameter options in animate(withDuration:delay:options:animations:completion:).

On the plus side, it allows you to use clean code, for example:

options: [.allowAnimatedContent, .curveEaseIn]

However, there are also disadvantages.

If I want to display the specified values OptionSet, there seems to be no clean way to do this:

let options: UIViewAnimationOptions = [.allowAnimatedContent, .curveEaseIn]
print("options = " + String(describing: options))

Displays a very useless message:

options = UIViewAnimationOptions (rawValue: 65664)

Documents for some of these bit fields express a constant as the value of the strength of two:

flag0    = Flags(rawValue: 1 << 0)

But the docs for my OptionSet example,, UIViewAnimationOptionstell you nothing about the numerical value of these flags and calculating bits from decimal numbers is not easy.

Question:

- OptionSet ?

:

options = UIViewAnimationOptions ([. allowAnimatedContent,.curveEaseIn])

, , .

( , , , .)

, , , OptionSets.

+11
3

NSHipster OptionSet, OptionSet, :

https://nshipster.com/optionset/

, Option CustomStringConvertible, . NSHipster - CustomStringConvertible Option

protocol Option: RawRepresentable, Hashable, CaseIterable, CustomStringConvertible {}

enum Topping: String, Option {
    case pepperoni, onions, bacon,
    extraCheese, greenPeppers, pineapple

    //I added this computed property to make the class conform to CustomStringConvertible
    var description: String {
        return ".\(self.rawValue)"
    }
}

extension Set where Element == Topping {
    static var meatLovers: Set<Topping> {
        return [.pepperoni, .bacon]
    }

    static var hawaiian: Set<Topping> {
        return [.pineapple, .bacon]
    }

    static var all: Set<Topping> {
        return Set(Element.allCases)
    }
}

typealias Toppings = Set<Topping>

extension Set where Element: Option {
    var rawValue: Int {
        var rawValue = 0
        for (index, element) in Element.allCases.enumerated() {
            if self.contains(element) {
                rawValue |= (1 << index)
            }
        }
        return rawValue
    }
}

:

let toppings: Set<Topping> = [.onions, .bacon]

print("toppings = \(toppings), rawValue = \(toppings.rawValue)")

toppings = [.onions,.bacon], rawValue = 6

, .

, , , description . description ( String) .

rawValue Set<Option> OptionSet , .

, Swift OptionSet s.

0

, , . , .

struct MyOptionSet: OptionSet, Hashable, CustomStringConvertible {

    let rawValue: Int
    static let zero = MyOptionSet(rawValue: 1 << 0)
    static let one = MyOptionSet(rawValue: 1 << 1)
    static let two = MyOptionSet(rawValue: 1 << 2)
    static let three = MyOptionSet(rawValue: 1 << 3)

    var hashValue: Int {
        return self.rawValue
    }

    static var debugDescriptions: [MyOptionSet:String] = {
        var descriptions = [MyOptionSet:String]()
        descriptions[.zero] = "zero"
        descriptions[.one] = "one"
        descriptions[.two] = "two"
        descriptions[.three] = "three"
        return descriptions
    }()

    public var description: String {
        var result = [String]()
        for key in MyOptionSet.debugDescriptions.keys {
            guard self.contains(key),
                let description = MyOptionSet.debugDescriptions[key]
                else { continue }
            result.append(description)
        }
        return "MyOptionSet(rawValue: \(self.rawValue)) \(result)"
    }

}

let myOptionSet = MyOptionSet([.zero, .one, .two])

// prints MyOptionSet(rawValue: 7) ["two", "one", "zero"]
+1

StrOptionSet:

  • , Self.

StrOptionSet:

  • , .
  • .
  • "," CustomStringConvertible :: description

:

protocol StrOptionSet : OptionSet, CustomStringConvertible {
    typealias Label = (Self, String)
    static var labels: [Label] { get }
}
extension StrOptionSet {
    var strs: [String] { return Self.labels
                                .filter{ (label: Label) in self.intersection(label.0).isEmpty == false }
                                .map{    (label: Label) in label.1 }
    }
    public var description: String { return strs.joined(separator: ",") }
}

Add a label set for the target VTDecodeInfoFlags parameter set .

extension VTDecodeInfoFlags : StrOptionSet {
    static var labels: [Label] { return [
        (.asynchronous, "asynchronous"),
        (.frameDropped, "frameDropped"),
        (.imageBufferModifiable, "imageBufferModifiable")
    ]}
}

Use it

let flags: VTDecodeInfoFlags = [.asynchronous, .frameDropped]
print("flags: ", flags)
0
source

Source: https://habr.com/ru/post/1671399/


All Articles