Convert NSArray to NSMutableArray Swift

You can do the same thing in swift, just modify the syntax a bit:

var arr = NSArray()
var mutableArr = NSMutableArray(array: arr)

With Swift 5, NSMutableArray has an initializer init(array:) that it inherits from NSArray. init(array:) has the following declaration:

convenience init(array anArray: NSArray)

Initializes a newly allocated array by placing in it the objects contained in a given array.


The following Playground sample code shows hot to create an instance of NSMutableArray from an instance of NSArray:

import Foundation

let nsArray = [12, 14, 16] as NSArray
let nsMutableArray = NSMutableArray(array: nsArray)
nsMutableArray.add(20)
print(nsMutableArray)

/*
prints:
(
    12,
    14,
    16,
    20
)
*/