Created
October 5, 2017 08:19
-
-
Save yoman07/8d24cc517a644006677b44fba4840fa6 to your computer and use it in GitHub Desktop.
Thanks to this code you can create dictionary with enums and range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Dictionary where Key == Range<Float> { | |
subscript(bmi bmi: Float) -> Value? { | |
guard let k = keys.filter({$0 ~= bmi}).first else { | |
return nil | |
} | |
return self[k] | |
} | |
} | |
import Foundation | |
extension Range : Hashable { | |
public var hashValue: Int { | |
return "\(lowerBound) to \(upperBound)".hashValue | |
} | |
} | |
enum ABCD { | |
case a, b, c, d | |
static let dict: [Range<Float>: ABCD] = [ | |
0.0 ..< 18.5: ABCD.a, | |
18.5 ..< 25.0: ABCD.b, | |
25.0 ..< 30: ABCD.c, | |
30 ..< MAXFLOAT: ABCD.d | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment