O'Reilly logo

iOS 8 SDK Development, 2nd Edition by Janie Clayton, Chris Adamson

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

Collections

As with strings, Swift moves support for the most common collections directly into the language, which makes them significantly easier to work with than the Objective-C approach of calling methods on objects.

Arrays

Swift provides direct language support for two essential collections: arrays and dictionaries. Arrays, as in most languages, are ordered lists of objects.

 
var musicGenres = ["Pop", "Rock", "Jazz", "Hip-hop", "Classical"]

Because of type inference, Swift knows this is an array of Strings. We could make that explicit by making the declaration var musicGenres: [String].

We can access array members by a zero-based index, inside square braces. We can also get a sub-array by using the range operator, where ..< includes the ...

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