Fix warning “C-style for Statement is deprecated” in Swift 3

C-style for loop has been deprecated in Swift 3. You can continue using it for a while, but they will certainly disappear in the future.

You can rewrite your loop to Swift’s style:

for i in 0..<len {
    let length = UInt32 (letters.length)
    let rand = arc4random_uniform(length)
    randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}

Since you don’t use i at all in the loop’s body, you can replace it with:

for _ in 0..<len {
    // do stuffs
}

Leave a Comment