iOS Swift max(), min() iOS Swift max(), min() ios ios

iOS Swift max(), min()


Don't pass anything at all there. It's a variadic function, you can call it with two or more parameters.


You can use the empty parameters for simple comparable data types, if you have a more complex data structures you need to provide a closure. Note: the order of the comparison are the following for both max and min if you do it backward you get wrong results.

[(0, "a"), (2, "b"), (5, "f"), (6, "g")].min(by: {$0.0 < $1.0}) // min (0, "a")[(0, "a"), (2, "b"), (5, "f"), (6, "g")].max(by: {$0.0 < $1.0}) // max (6, "g")