I'd like to know how can I limit the set of values that I can pass to function as an argument (or to class as a property). Or, in other words, the logic that I want to implement, actually, is to make function or a class accept only particular values. I've come up with an idea to use enum for it. But the caveat here is that I can't use pure integers with the 'case' like so:
enum Measure {
case 1, 2, 3
}
Is there any way to implement what I want?
enum Measure:Int{
case ONE = 1
case TWO = 2
case THREE = 3
}
//accept it as argument
func myMethod(measure:Measure){
switch measure {
case .ONE:...
case .TWO:...
case .THREE
}
}
//call the method with some raw data
myMethod(Measure(rawValue:1)!)
//or call the method with
myMethod(Measure.ONE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With