Reputation: 3789
I have a group of UIImageViews that are stored in an array. I want to animate a certain amount of these ImageViews in the same way. Thus, I have been trying to use the following code:
var lowerViews: [UIImageView] = [imageView1, imageView2, imageView3, imageView4]
var startingIndex = 1;
UIView.animateWithDuration(0.3, delay: 0.1, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
for index in startingIndex..< lowerViews.count {
lowerViews[index].frame.origin.y += 100
}
}, completion: nil)
However at this line:
for index in startingIndex..< lowerViews.count {
Xcode gives me the error:
Expected '{' to start the body of each for-loop
However, I don't believe that this is really the issue. This seems to me that it is an arbitrary syntax error that Xcode gets because I am using the for-loop inside the 'animation' parameter. Since I am still learning a lot about Swift I do not know why this wouldn't work, and so if this assumption is correct, I would like to know why and how I can get around this.
If this is not the case, please let me know, because either way I need to get around the problem.
Thanks in advance
Upvotes: 0
Views: 570
Reputation: 130102
This is a tricky error (note spaces around ..<
).
for index in startingIndex ..< lowerViews.count {
will work or
for index in startingIndex..<lowerViews.count {
will work but:
for index in startingIndex..< lowerViews.count {
won't work.
The reason for this is the fact that when startingIndex..<
is used, then ..<
is considered to be a postfix (unary) operator (not an infix operator). Therefore the whole expression stops making sense and you start getting strange errors.
Also see what are the rules for spaces in swift
Upvotes: 4