Hundreds of for loop error

Today when I opened a old project with xCode 8.1, more than 300 errors related to for loop appeared. For the code as below,


for (i in 0 ..< (dataSets?.count)!)

{


It complained "Expected "," separator", "Expected in after for-each pattern". How can I fix this? What's the best way to correct all 300 locations?

Replies

Remove the redundant parentheses:


for i in 0 ..< (dataSets?.count)!
{ … }


You should also submit a bug report about the unhelpful error message.


P.S. Unrelated, but I think it's equivalent to write the upper bound in a simpler (and easier-to-grasp) form:


for i in 0 ..< dataSets!.count
{ … }

Unrelated, but I think it's equivalent to write the upper bound in a simpler (and easier-to-grasp) form:

Or if

dataSets
is any normal Swift collection
for i in dataSets!.indices {
    …
}

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

But how am I supposed to manually correct all 300 errors?

Is this related to swift 2.3 vs swift 3.0? I have not issue to build the project before.

I\'ve always had problems if the "if" expression was in parentheses, but the rules might have changed slightly since 2.2/2.3.


You should be able to fix this with a global search and replace using a regular expression.

Thanks a lot for the information.


Could you delibrate how to fix it with regular expression?

Open your project in Xcode.

Find > Find and Replace in Project...


Find regex:

for\s*\((.*)\)(\s*)\{

Replace with:

for $1 $2{


Press Enter key to perform search.

Click Preview button and inspect the proposed changes.

Decide whether to proceed with some or all changes, or cancel and edit the find and/or replace terms before previewing again.