I have an enumeration that represents the thickness of a line, which can be either a constant width or a width relative to the size of the view.
enum Thickness {
case Constant(Float)
case Relative(Float)
}
Is there a way to build these related value constraints into an enum type? I am currently using didSetproperty observers for properties of this type:
var lineThickness: Thickness {
didSet {
switch lineThickness {
case let .Relative(x): lineThickness = .Relative(min(max(x, 0), 1))
case let .Constant(x): lineThickness = .Constant(max(x, 0))
}
}
}
But it would be far ahead if I could do this once for the whole type and not replicate this observer for each property.
I know that I can create initializers or methods for enumeration, but I'm not sure that / how could I use this to limit the bound value?
source
share