O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Quick Note on Type Inference

Take a look at this code that you entered earlier:

for i in 1...5 {
    myFirstInt += 1
    print("myFirstInt equals \(myFirstInt) at iteration \(i)")
}

Notice that i is not declared to be of the Int type. It could be, as in for i: Int in 1...5, but an explicit type declaration is not necessary. The type of i is inferred from its context (as is the let). In this example, i is inferred to be of type Int because the specified range contains integers.

Type inference is handy: Because you will type less, you will make fewer typos. However, there are a few cases in which you need to specifically declare the type. We will highlight those when they come up. In general, however, we recommend that you take advantage ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required