Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the minimum value of two CGFloats in Swift? [duplicate]

Tags:

ios

swift

cgfloat

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?

like image 228
Amit Erandole Avatar asked Oct 28 '25 16:10

Amit Erandole


2 Answers

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.

like image 121
Rashad Avatar answered Oct 31 '25 06:10

Rashad


The function in Swift is min, so:

var minScale = min(xScale, yScale)

like image 38
fqdn Avatar answered Oct 31 '25 07:10

fqdn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!