john john - 2 months ago 15
Swift Question

Swift 3.0 error with C-style for loop

I converted my project to Swift 3.0 and I get this error at the

for
line. Please see the error image below. After I converted it gave me a C-style error.

func removeButton(_ aButton: UIButton) {
var iteration = 0
var iteratingButton: UIButton? = nil
for( ; iteration<buttonsArray.count; iteration += 1) {
iteratingButton = buttonsArray[iteration]
if(iteratingButton == aButton) {
break
} else {
iteratingButton = nil
}
}
if(iteratingButton != nil) {
buttonsArray.remove(at: iteration)
iteratingButton!.removeTarget(self, action: #selector(SSRadioButtonsController.pressed(_:)), for: UIControlEvents.touchUpInside)
if currentSelectedButton == iteratingButton {
currentSelectedButton = nil
}
}
}


Error image here

Answer

It's a clear error, Swift 3 does NOT support the c-style for loop.

Instead, you should:

    for iteration in 0 ..< buttonsArray.count {
        iteratingButton = buttonsArray[iteration]
        if(iteratingButton == aButton) {
            break
        } else {
            iteratingButton = nil
        }
    }

You should check Apple's Documentation.