Elegant `bounded` methodology in Swift

前端 未结 3 1662
悲哀的现实
悲哀的现实 2021-01-23 11:09

I\'m looking for a more elegant way to create bounded limiters for numbers, primarily to be used in setters. There are plenty of techniques for determining whether a value falls

3条回答
  •  温柔的废话
    2021-01-23 11:30

    This is Apple's own approach, taken from this sample code:

    func clamp(value: T, minimum: T, maximum: T) -> T {
        return min(max(value, minimum), maximum)
    }
    

提交回复
热议问题