O'Reilly logo

Xcode 7 Essentials - Second Edition by Jayant Varma, Brett Ohland

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

Collection types

Swift offers three types of object that can be used to store collections of data: arrays, dictionaries, and sets:

  • Arrays: This are used to store an ordered list of objects of the same type
  • Dictionaries: This are used to store unordered data but individual items are easy to access
  • Sets: This are used to store unordered lists of items in which there can be no duplicates

Unlike Objective-C, the types of value that you can store within a Swift collection need to be declared when created and any attempt to store a different type will cause an error, for example:

var pastaNames:[String] = ["Rotini", "Spagetti", "Macaroni"]
pastaNames.append(10000) // Error

The pastaNames array was declared to only contain strings; attempting to append an ...

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