Standard way to “clamp” a number between two values in Swift
Swift 4/5 Extension of Comparable/Strideable similar to ClosedRange.clamped(to:_) -> ClosedRange from standard Swift library. extension Comparable { func clamped(to limits: ClosedRange<Self>) -> Self { return min(max(self, limits.lowerBound), limits.upperBound) } } #if swift(<5.1) extension Strideable where Stride: SignedInteger { func clamped(to limits: CountableClosedRange<Self>) -> Self { return min(max(self, limits.lowerBound), limits.upperBound) } } #endif Usage: 15.clamped(to: 0…10) … Read more