104

In C / Objective-C it is possible to find the minimum and maximum value between two numbers using MIN and MAX macros. Swift doesn't support macros and it seems that there are no equivalents in the language / base library. Should one go with a custom solution, maybe based on generics like this one?

0

5 Answers 5

134

min and max are 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

and used like so:

let min = min(1, 2)
let max = max(1, 2)

See this great writeup on documented & undocumented built-in functions in Swift.

6
  • 2
    Sorry. How did you learn this? I don't see it in 'The Swift Programming Language' nor in an Xcode6 documentation search.
    – GoZoner
    Commented Jun 12, 2014 at 14:35
  • 7
    @GoZoner Sometimes you just have to type it up in Xcode and check out the Swift header. If you type let a : Int = 5 and Command + Click on Int, you get to see cool stuff!
    – Jack
    Commented Jun 12, 2014 at 14:37
  • Wow, glad I asked! At best I had been typing a. and scrolling through the Xcode completion possibilities... but 'Command + Click' is the ticket!
    – GoZoner
    Commented Jun 12, 2014 at 14:44
  • 1
    @GoZoner Autocomplete is a great place to search too! Unfortunately, its very wonky for Xcode 6 at the moment...
    – Jack
    Commented Jun 12, 2014 at 14:47
  • 1
    The code snippet contains a lot of noise ( <T : Comparable> ? rest: T... ? What do those mean?) and doesn't really explain how to use the function. It'd be much more helpful to have a line of code actually showing its use. Commented Mar 28, 2018 at 5:54
39

As pointed out, Swift provides max and min functions.

An 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
2
  • Int() has the value of 0 so if your numbers are negative this won't work
    – Sentry.co
    Commented Sep 1, 2016 at 20:18
  • 1
    you could make the maxNumber optional and replace the for loop with this: numbers.forEach{maxNumber = maxNumber != nil ? max(maxNumber!,$0) : $0}
    – Sentry.co
    Commented Sep 1, 2016 at 20:27
25

With Swift 5, max(_:_:) and min(_:_:) are part of the Global Numeric Functions. max(_:_:) has the following declaration:

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

You can use it like this with Ints:

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

Also note that there are other functions called max(_:_:_:_:) and min(_:_:_:_:) that allows you to compare even more parameters. max(_:_:_:_:) has the following declaration:

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

You can use it like this with Floats:

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

With Swift however, you're not limited to use max(_:_:) and its siblings with numbers. In fact, those functions are generic and can accept any parameter type that conforms to Comparable protocol, may it be String, Character or one of your custom class or struct.

Thereby, the following Playground sample code works perfectly:

class Route: Comparable, CustomStringConvertible {

    let distance: Int
    var description: String {
        return "Route with distance: \(distance)"
    }

    init(distance: Int) {
        self.distance = distance
    }

    static func ==(lhs: Route, rhs: Route) -> Bool {
        return lhs.distance == rhs.distance
    }

    static 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"

Furthermore, if you want to get the min/max element of elements that are inside an Array, a Set, a Dictionary or any other sequence of Comparable elements, you can use the max() or the min() methods (see this Stack Overflow answer for more details).

1
  • 2
    Very helpful, thanks! Much better than the answer above since you actually give an example of usage, rather than a cryptic header. Commented Mar 28, 2018 at 5:55
15

SWIFT 4 Syntax 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

So when you use it you should write like in this example:

let min = 0
let max = 100
let value = -1000

let currentValue = Swift.min(Swift.max(min, value), max)

So you get the value from 0 to 100 don't matter if is it below 0 or higher 100.

1
  • 2
    If you're getting a fun compiler error in XCode 9 like me, saying that max() doesn't exist even when you clearly pass it two Ints, then change it to Swift.max and suddenly yay, things are better. Thanks wm.p1us!
    – xaphod
    Commented Nov 29, 2017 at 20:03
0

Try this:

let numbers = [2, 3, 10, 9, 14, 6]
let min = numbers.min()
let max = numbers.max()
print("Max = \(max) Min = \(min)")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.