Fast equivalent for MIN and MAX macros

In C / Objective-C, you can find the minimum and maximum values ​​between two numbers using the MIN and MAX macros. Swift does not support macros and it seems that there are no equivalents in the language / database library. If you go with a special solution, perhaps based on generics such as one ?

+84
generics swift
Jun 12 '14 at 14:13
source share
5 answers

min and max already defined in Swift:

 func max<T : Comparable>(x: T, y: T, rest: T...) -> T func min<T : Comparable>(x: T, y: T, rest: T...) -> T 

Check out this great entry for documented and undocumented built-in functions in Swift .

+114
Jun 12 '14 at 14:17
source share

As indicated, Swift provides the max and min functions.

Example (updated for Swift 2.x).

 let numbers = [ 1, 42, 5, 21 ] var maxNumber = Int() for number in numbers { maxNumber = max(maxNumber, number as Int) } print("the max number is \(maxNumber)") // will be 42 
+28
Aug 22 '14 at 19:51
source share

With Swift, min and max are part of the Swift Standard Reference Functions Reference .

max(_:_:) has the following declaration:

 func max<T : Comparable>(_ x: T, _ y: T) -> T 

You can use it like this: Int :

 let maxInt = max(5, 12) // returns 12 



There is a second function called max(_:_:_:_:) , which allows you to compare even more parameters. max(_:_:_:_:) takes a variational parameter and has the following declaration:

 func max<T : Comparable>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T 

You can use it like this: Float :

 let maxInt = max(12.0, 18.5, 21, 15, 26, 32.9, 19.1) // returns 32.9 



However, with Swift, you are not limited to using max(_:_:) , max(_:_:_:_:) and their min copies with Int , Float or Double . In fact, these functions are generic and can take any type of parameter that conforms to the Comparable protocol, maybe String , Character or one of your custom class or struct . Thus, the following site codes work great:

 let maxString = max("Car", "Boat") // returns "Car" (alphabetical order) 
 class Route: Comparable, CustomStringConvertible { let distance: Int var description: String { return "Route with distance: \(distance)" } init(distance: Int) { self.distance = distance } } func ==(lhs: Route, rhs: Route) -> Bool { return lhs.distance == rhs.distance } func <(lhs: Route, rhs: Route) -> Bool { return lhs.distance < rhs.distance } let route1 = Route(distance: 4) let route2 = Route(distance: 8) let maxRoute = max(route1, route2) print(maxRoute) // prints "Route with distance: 8" 



In addition, if you want to get the maximum element of elements that is inside Array , a Set , a Dictionary or any other sequence, you can use maxElement () or the maxElement (_ :) methods . See this answer for more details.

+15
Nov 26 '15 at 12:11
source share

SWIFT 4 The syntax has changed a bit:

 public func max<T>(_ x: T, _ y: T) -> T where T : Comparable public func min<T>(_ x: T, _ y: T) -> T where T : Comparable 

and

 public func max<T>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T where T : Comparable public func min<T>(_ x: T, _ y: T, _ z: T, _ rest: T...) -> T where T : Comparable 

Therefore, when you use it, you should write, as in this example:

 let min = 0 let max = 100 let value = -1000 let currentValue = Swift.min(Swift.max(min, value), max) 

So, you get a value from 0 to 100, it does not matter if it is below 0 or above 100.

+4
Sep 26 '17 at 4:09 on
source share

Try it.

  let numbers = [2, 3, 10, 9, 14, 6] print("Max = \(numbers.maxElement()) Min = \(numbers.minElement())") 
0
May 13 '16 at 7:05
source share



All Articles