What is a "(UIView) -> CGFloat"? I am trying to get a CGFloat value

160 Views Asked by At
extension UIScrollView {
    func scrollPositionY(view:UIView) -> CGFloat {
        if let origin = view.superview {
            // Get the Y position of your child view
            let childStartPoint = origin.convert(view.frame.origin, to: self)

            let this = childStartPoint.y

            return this
        }
    }
}

and then

let theYvalue = theScrollView.scrollPositionY

theYvalue is of type (UIView) -> CGFloat

Can I convert this to a CGFloat?

Thank you!

2

There are 2 best solutions below

0
George On BEST ANSWER

You didn't call the function. Functions are called using (), and because you have parameters, arguments need to be passed in, like (view: someViewPassedIn) where someViewPassedIn must be of type UIView.

In Swift, the difference between functions and closures is very small. Variables can be set as a closure, which is what (UIView) -> CGFloat is. It is a closure that has not been called like a function.

It can be quite hard to wrap your head around, but basically closures contain the block of code between the curly brackets/braces, {}, and the () runs that block of code.

Instead of:

let theYvalue = theScrollView.scrollPositionY

Try:

let theYvalue = theScrollView.scrollPositionY(view: someViewPassedIn)
0
HalR On
let theYvalue = theScrollView.scrollPositionY

Just assigns the function to theYvalue

I think what you want is

let theYvalue = theScrollView.scrollPositionY()

which evaluates the function and should be a CGFloat, which the function returns.