For-In Loops multiple conditions

Viewed 11520

With the new update to Xcode 7.3, a lot of issues appeared related with the new version of Swift 3. One of them says "C-style for statement is deprecated and will be removed in a future version of Swift" (this appears in traditional for statements).

One of this loops has more than one condition:

for i = 0; i < 5 && i < products.count; i += 1 {

}

My question is, is there any elegant way (not use break) to include this double condition in a for-in loop of Swift:

for i in 0 ..< 5 {

}
5 Answers

One more example. Loop through all UILabel in subviews:

for label in view.subviews where label is UILabel {
    print(label.text)
}

UPDATE

Now the subview inside the loop is UIView and needs to additionally case it to UILabel

for subview in view.subviews where subview is UILabel {
    let label = subview as? UILabel
    print("\(label?.text ?? "")")
}
Related