Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ".." with for loop

There is a code snippet in the swift book provided by Apple. I typed it in xcode playground but it shows "Operator is not a known binary operator" and "Use of unresolved identifier" errors. The code is:

Xcode Playground code According to the book, ".." operator can be used with for loop to make a range that omits its upper value. But it displays error. Changing ".." to "..." operator seems to fix the error. But why does not ".." work?

like image 277
Kiran Thapa Avatar asked Oct 22 '14 13:10

Kiran Thapa


People also ask

Can we use continue statement With both for and while loop?

The continue statement can be used in both while and for loops.

What are the 3 types of loops?

Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met. Visual Basic has three main types of loops: for.. next loops, do loops and while loops.

What is correct syntax of for loop?

A: Syntax of for loop in c++ Language is for(initialization;condition;increment/decrement) Option (A)… Q: Q1) True/False questions: Write T for true andF for false statement.

Can we use and in for loop in Python?

You can put a for loop inside a while, or a while inside a for, or a for inside a for, or a while inside a while. Or you can put a loop inside a loop inside a loop. You can go as far as you want.


2 Answers

You have an out of date version of the swift book.

Here you go. The current version of the half-closed range is 0..<3 not 0..3 https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/BasicOperators.html

like image 91
Fogmeister Avatar answered Oct 12 '22 01:10

Fogmeister


Things are now different in Swift 2. Here's how to do it now in Xcode 7 (the original question shows highly in a google search for this particular problem):

for i in 1 ..< Process.argc {
    let index = Int(i);

    if let arg = String.fromCString(Process.unsafeArgv[index]) {
        switch arg {
        case "-whatever":
            // do something

        default:
            break
        }
    }
}
like image 39
Marc Fearby Avatar answered Oct 12 '22 00:10

Marc Fearby