3

I have this code that should return the minimum value of two CGFloat values:

    var boundsSize:CGSize = scrollView.bounds.size
    var imageSize:CGSize = imageView.bounds.size

    var xScale:CGFloat = boundsSize.width / imageSize.width
    var yScale:CGFloat = boundsSize.height / imageSize.height

    var minScale:CGFloat = MIN(xScale, yScale) // this does not work

But MIN does not seem to have an equivalent in swift. So how do I do this?

Amit Erandole
  • 11,995
  • 23
  • 65
  • 103

2 Answers2

7

Try this:

var minScale = min(xScale, yScale)

min is already in swift. Hope this helps.. :)

min is variadic function. Its reset parameter is a variadic parameter. From developer site:

A variadic parameter accepts zero or more values of a specified type. You use a variadic parameter to specify that the parameter can be passed a varying number of input values when the function is called. Write variadic parameters by inserting three period characters (...) after the parameter’s type name.

For more info. See this.

Rashad
  • 11,057
  • 4
  • 45
  • 73
4

The function in Swift is min, so:

var minScale = min(xScale, yScale)

fqdn
  • 2,823
  • 1
  • 13
  • 16