0

I have the following code:

let something = 5

for (var i = 1; i< something; i++)
{

}

I get the following error:

braced block in unused closure!!

There is something wrong with the "something" variable I am using above. Any ideas?

UPDATE: 

And this works:

 for var i = 1; i<=noOfTimes;i++
    {
        println("Hello World")
    }
1
  • Could it have anything to do with the fact that your first example has a closure with no actual code in it? Commented Sep 7, 2014 at 0:36

3 Answers 3

2

In your for loop, put a space between i and < or remove the space between < and something. The good syntaxes are: i < something and i<something.

This is the result of some powerful features in Swift: prefix, postfix & compound operators.

Sign up to request clarification or add additional context in comments.

2 Comments

@Zaph Well, it's defined that way in the Swift book; see the bit about whitespace in the Operators section of the Lexical Structure chapter. It's the way the compiler determines the difference between a postfix operator and a binary operator here (as in Swift, "i<" might be "i" with the postfix operator "<" applied.)
The point is that it is a poor language design when such a minor change causes a major change in program execution. It is a flaw in the design of the language.
0

Swift supports two syntaxes for for:

for <identifier> in <collection> { <statements> }

or

for <initialization>; <condition>; <increment> { <statements> }

Neither of them require parenthesis around the for specification.

Comments

0

You are missing a space between i and <. The following works.

let something = 5

for (var i = 1; i < something; i++)
{

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.