I have a enum that has an integer representation, and I am going to be iterating over this type in a for loop. I was loop to get both the min and max int value from the enum
private enum PreloadedArrayDataIndex: Int, CaseIterable {
case Previous = 0
case Current = 1
case Future = 2
}
in this case, the min should return 0, for .Previous, and would return 2 for .Future.
I was looking to see if there is an easy 'swift' way to do this, something like
let minValue = PreloadedArrayDataIndex.allCases.min
let maxValue = PreloadedArrayDataIndex.allCases.max
I know that I could iterate over all the cases, and check each value against a stored max, but was looking to see if there was a different way I was not aware of.
The integers 0, 1, 2 are the “raw values” of the enumeration, and you can get the smallest and largest of the raw values with
Another option is to make the enumeration comparable, so that you can determine the smallest and largest case: