Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable/Mutable Collections in Swift

I was referring to Apple's Swift programming guide for understanding creation of Mutable/ immutable objects(Array, Dictionary, Sets, Data) in Swift language. But I could't understand how to create a immutable collections in Swift.

I would like to see the equivalents in Swift for the following in Objective-C

Immutable Array

NSArray *imArray = [[NSArray alloc]initWithObjects:@"First",@"Second",@"Third",nil]; 

Mutable Array

NSMutableArray *mArray = [[NSMutableArray alloc]initWithObjects:@"First",@"Second",@"Third",nil]; [mArray addObject:@"Fourth"]; 

Immutable Dictionary

NSDictionary *imDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Value1", @"Key1", @"Value2", @"Key2", nil]; 

Mutable Dictionary

NSMutableDictionary *mDictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Value1", @"Key1", @"Value2", @"Key2", nil]; [mDictionary setObject:@"Value3" forKey:@"Key3"]; 
like image 749
Pranav Jaiswal Avatar asked Jun 07 '14 10:06

Pranav Jaiswal


People also ask

What is mutable and immutable in Swift?

In Swift, a variable is mutable whereas a constant is immutable. Xcode suggests turning the numberOfWheels constant into a variable by replacing the let keyword with the var keyword. In other words, Xcode suggests making numberOfWheels mutable. The value stored in a variable can be changed.

Are lists mutable in Swift?

If you create an array, a set, or a dictionary, and assign it to a variable, the collection that's created will be mutable. This means that you can change (or mutate) the collection after it's created by adding, removing, or changing items in the collection.

Is array mutable in Swift?

If you assign a created array to a variable, then it is always mutable, which means you can change it by adding, removing, or changing its items; but if you assign an array to a constant, then that array is immutable, and its size and contents cannot be changed.


2 Answers

Arrays

Create immutable array

First way:

let array = NSArray(array: ["First","Second","Third"]) 

Second way:

let array = ["First","Second","Third"] 

Create mutable array

var array = ["First","Second","Third"] 

Append object to array

array.append("Forth") 


Dictionaries

Create immutable dictionary

let dictionary = ["Item 1": "description", "Item 2": "description"] 

Create mutable dictionary

var dictionary = ["Item 1": "description", "Item 2": "description"] 

Append new pair to dictionary

dictionary["Item 3"] = "description" 

More information on Apple Developer

like image 97
nicael Avatar answered Oct 18 '22 15:10

nicael


Swift does not have any drop in replacement for NSArray or the other collection classes in Objective-C.

There are array and dictionary classes, but it should be noted these are "value" types, compared to NSArray and NSDictionary which are "object" types.

The difference is subtle but can be very important to avoid edge case bugs.

In swift, you create an "immutable" array with:

let hello = ["a", "b", "c"] 

And a "mutable" array with:

var hello = ["a", "b", "c"] 

Mutable arrays can be modified just like NSMutableArray:

var myArray = ["a", "b", "c"]  myArray.append("d") // ["a", "b", "c", "d"] 

However you can't pass a mutable array to a function:

var myArray = ["a", "b", "c"]  func addToArray(myArray: [String]) {   myArray.append("d") // compile error } 

But the above code does work with an NSMutableArray:

var myArray = ["a", "b", "c"] as NSMutableArray  func addToArray(myArray: NSMutableArray) {   myArray.addObject("d") }  addToArray(myArray)  myArray // ["a", "b", "c", "d"] 

You can achieve NSMutableArray's behaviour by using an inout method parameter:

var myArray = ["a", "b", "c"]  func addToArray(inout myArray: [String]) {   myArray.append("d") }  addToArray(&myArray)  myArray // ["a", "b", "c", "d"] 

Re-wrote this answer 2015-08-10 to reflect the current Swift behaviour.

like image 33
Abhi Beckert Avatar answered Oct 18 '22 15:10

Abhi Beckert