Skip to content

Instantly share code, notes, and snippets.

@yoman07
Created October 5, 2017 08:19
Show Gist options
  • Save yoman07/8d24cc517a644006677b44fba4840fa6 to your computer and use it in GitHub Desktop.
Save yoman07/8d24cc517a644006677b44fba4840fa6 to your computer and use it in GitHub Desktop.
Thanks to this code you can create dictionary with enums and range
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