I want to create an enum value like
view.autoresizingMask = [UIViewAutoresizing.FlexibleBottomMargin, UIViewAutoresizing.FlexibleHeight]
I can not figure out to to design the enum
to accept multiple variable.
I want to design the below.
enum ActivityType: Int {
case Event = 1
case CommentOnEvent = 2
case CommentOnComment = 3
}
var activityType = [CommentOnEvent, CommentOnComment]
var activityType = Event
It's not an enum
, it's a struct
that conforms to OptionSetType
. You can create one following this pattern:
struct AnimalTraits : OptionSetType {
let rawValue: Int
init(rawValue: Int) { self.rawValue = rawValue }
static let LaysEggs = AnimalTraits(rawValue: 1)
static let GivesLiveBirth = AnimalTraits(rawValue: 2)
static let HasFur = AnimalTraits(rawValue: 4)
static let HasScales = AnimalTraits(rawValue: 8)
static let Mammal: AnimalTraits = [GivesLiveBirth, HasFur]
static let Fish: AnimalTraits = [LaysEggs, HasScales]
}
Then you can use it like so:
struct Animal {
let name: String
let traits: AnimalTraits
}
let salmon = Animal(name: "Salmon", traits: .Fish)
salmon.traits.contains(.HasFur) //false
salmon.traits.contains(.HasScales) //true
let bizarro = Animal(name: "Fictional Animal", traits: [.LaysEggs, .GivesLiveBirth])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.