Search code examples
swiftfor-loopswift3xcode8anyobject

Syntax changes in Swift 3 with Xcode 8


Recently, I convert to swift 3 and many errors were occurring.
Now I convert each lines of codes to swift 3. :(
I would like to know how to write loop in swift 3? Here it is ...

for index in 0...((data as AnyObject).count)!-1{
            print("Hello Everyone")}

Could not print the message. May be the syntax of loop in swift 3 changed.
Please help me.


Solution

  • Better you can try the Xcode's Swift migration assistance, that will help reduce your effort & time to convert the code to swift 3 :

    When you open your project with Xcode 8.0 for the first time, you will be prompted via the migration assistant to do a migration pass. The assistant can also be invoked manually from the menu Edit -> Convert -> To Current Swift Syntax…

    Also, the problem seems to be in your expression, try following :

    for index in 0...((data as! [AnyObject]).count-1){ // will work if "data" is non-nil and is an array
            print("Hello Everyone")}