Search code examples
animationswiftclosuresblock

Access variables for closures inside functions in swift


in the code below I am unable to access the value of 'finished' the way we used to do in objective C.

UIView.animateWithDuration(5, delay: 5, options: UIViewAnimationOptions.AllowAnimatedContent, animations: { () -> Void in

        }, completion: { (finished:Bool) -> Void in

    });

Thanks in advance!


Solution

  • Looks like this one got mostly figured out in comments, but let's give it a clear answer for the benefit of those who might run across this later when trying to solve their own problems...

    Code completion doesn't fully work inside of closures as of Xcode 6.1. You can still use any identifiers that are in scope within the closure, you just don't get them suggested and pasted for you when you start typing. (It's a good reason to pay attention to the language, if you ask me... relying too much on tools keeps you from understanding your own code, and if you don't understand your own code, how are you going to make sure it's correct?) You're welcome to file a bug, but I imagine Apple already knows about this one.

    (Yes, Apple needs to improve MS Visual Studio's code completion feature. Wait, what? :D Xcode's code completion feature doesn't have a special marketing name.)