Peter Pik Peter Pik - 3 months ago 26
Objective-C Question

Convert NSArray to NSMutableArray Swift

I'm trying to convert a the

self.assets
NSArray
to
NSMutableArray
and add it to
picker.selectedAssets
which is a
NSMutableArray
. How will this code look like in swift?

Objective-C Code

picker.selectedAssets = [NSMutableArray arrayWithArray:self.assets];

Answer

NSMutableArray has an initializer convenience init(array anArray: NSArray) that it inherits from NSArray.

Therefore, the following Playground code works fine:

import Foundation

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

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