Swift-style-guide: syntax to use instead of c-style for loop

Created on 5 Apr 2016  ·  6Comments  ·  Source: raywenderlich/swift-style-guide

c-style for loops are getting sacked in Swift 3 and it's a good idea to comply as soon as possible.

swift collection types actually provide a list of their indexes no matter of the index type so I say we should use those across tutorials and books. E.g:

not preferred:
for var i=0;i<items.count;i++ { ..items[i]... }

preferred:
for i in items.indices { ... items[i] ... }

@micpringle ?

Most helpful comment

@khalidmahmud

for i in 0.stride(to: items.count, by: 2) {
    // items[i]
}

All 6 comments

👍

I'm for avoiding using C-style loops.

However, this will already generate a warning in Xcode 7.3 if you use them.

As we already don't allow warnings, does this really need to be explicitly included?

the new loop looks great
but for the loop of this type what should be done?
for var i=0;i what are the possible way to implement this in
for .... in ?

@khalidmahmud

for i in 0.stride(to: items.count, by: 2) {
    // items[i]
}

@yas375
thanks bro

Approved. Adding a section about compiler warnings being verboten. Also will change the current not preferred examples to use a while loop.

Apologies that there might be some noise here. I guess I am learning the github pull request workflow. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hollance picture hollance  ·  28Comments

gokselkoksal picture gokselkoksal  ·  9Comments

designatednerd picture designatednerd  ·  22Comments

Lweek picture Lweek  ·  5Comments

jrturton picture jrturton  ·  3Comments