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 ...

Get iOS 8 SDK Development, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.