Differences Between NSArrays and Swift Arrays

Did you notice in the preceding section that you don’t have mutable and immutable versions of the Swift array? Mutable means that something can be changed and immutable means it cannot be changed. An immutable array cannot change once it is created. Well, you really do have mutable and immutable versions of the array and every other variable, but you don’t need two different classes for each. To make an immutable array in Swift, you just assign it to a constant with let. If you want to make a mutable array in Swift, you just assign it to a variable using the keyword var:

var mutableArray = [1,2,3,4,5]let immutableArray = [1,2,3,4,5]

The following is a comparison of Swift ...

Get Learning Swift™ Programming 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.