Immutable Arrays

You have been doing a lot of tinkering with your bucket list array. But you can also create an array that cannot be changed. You use immutable arrays for these cases. Here is how.

Let’s say you are making an application that allows users to keep track of the lunches they eat each week. Among other things, users will log what they ate and generate reports at a later time. You decide to put these meals in an immutable array to generate the reports. After all, it does not make sense to change last week’s lunches after they have been eaten.

Create an immutable array and initialize it with a week’s worth of lunches.

Listing 9.17  An immutable array

... var myronsList = [ "Climb Mt. Kilimanjaro", "Fly hot air balloon ...

Get Swift Programming: The Big Nerd Ranch Guide 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.