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

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, called an immutable array. 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

...
let lunches = [
               "Cheeseburger",
               "Veggie Pizza",
               "Chicken Caesar Salad",
               "Black ...

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