swift - Build WatchOS Apps: Develop and Design -
i trying run sample code book build watchos: develop , design. following segment of code returns 2 errors:
@ibaction func buttontapped(){ if animating { spinnerimage.stopanimating() animating = false animatewithduration(0.2, animations: updatebuttontostopped()) } else { spinnerimage.startanimating() animating = true animatewithduration(0.2, animations: updatebuttontogoing()) } }
both errors occur in calls animatewithduration() , indicate there type conflict. ideas of how can fix this?
in hurry?
instead of calling animatewithduration
so:
animatewithduration(0.2, animations: updatebuttontostopped())
you'd give updatebuttontostopped
function parameter so:
animatewithduration(0.2, animations: updatebuttontostopped)
notice () after updatebuttontostopped
gone.
and same goes updatebuttontogoing
of course :)
why?
if @ documentation animatewithduration
, (which can see swift 3 version of here) can see signature looks this:
func animate(withduration duration: timeinterval, animations: @escaping () -> void)
the animations
interesting part here.
() -> void
means animations
takes function, must contain no parameters , returns void
.
in case call so:
animatewithduration(0.2, animations: updatebuttontostopped())
but...when use updatebuttontostopped()
, saying is, "call updatebuttontostopped()
, use the output of animations
parameter". not compiler expects, expects, saw, function, taking no parameters , returning void
.
so when say:
animatewithduration(0.2, animations: updatebuttontostopped)
without parentheses means not invoke updatebuttontostopped
, pass animate
parameter.
hope helps you.
Comments
Post a Comment