Reputation: 103
I'm getting this error "Cannot invoke '+' wit an argument list of type '(CGFloat, CGFloat)' when using this Swift code.
var added = CGFloat(gestureRecognizer.view?.center.y) + CGFloat(translation.y)
What am I doing wrong. I have two floats and I want to add them together.
Upvotes: 0
Views: 804
Reputation: 187134
This line here:
gestureRecognizer.view?.center.y
Can be thought of as an expression that returns a CGFloat?
optional. If view
is nil
, it returns nil
. If view
has a value, it drills in. And CGFloat(nil)
is a compile error.
var added = CGFloat(gestureRecognizer.view!.center.y) + CGFloat(translation.y)
// ^ ! instead of ?
You really want to unwrap it here. Because if view
is nil
you pass nil
to CGFloat()
which isn't gonna be cool. But you do have to think about what might happen when view
is nil
.
Perhaps that will never happen in the flow of your program, I don't know. But if it is nil
that will cause a runtime crash.
Upvotes: 3