Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

let or struct vs enum

enum category: UInt32 {
    case fence = 1
    case paddle = 2
    case block = 4
    case ball = 8
}

struct category{
    static var fence:UInt32 = 1
    static var paddle:UInt32 = 2
    static var block:UInt32 = 4
    static var category:UInt32 = 8
}

trying to create a set of bitMasks, I am wondering which one is the proper way to do this.

enum or struct ?

like image 638
Eric Yu Avatar asked Feb 19 '26 16:02

Eric Yu


1 Answers

What you want is an Option Set. That is Swift's way of letting you build a bitmask, such that you can then manipulate it like a set, which is really nice.

There's a fine example in the Swift docs:

https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_OptionSetType_Protocol/index.html

Newer version here:

https://developer.apple.com/reference/swift/optionset

like image 165
matt Avatar answered Feb 24 '26 15:02

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!