Reputation: 1841
I'm having trouble declaring a NSMutableArray in Swift 3 because using legacy objective C code bridges the swift Array
data type to NSArray
. Here's what I'm trying to do:
var myMutableArray: NSMutableArray = [myObjectCustomClass]
But I'm getting a compiling error:
Cannot convert value of type '[myObjectCustomClass].Type' (aka 'Array.Type' to specified type 'NSMutableArray'
I've tried a few other ways as well such as:
var myMutableArray = NSMutableArray<myObjectCustomClass>
var myMutableArray:[myObjectCustomClass] = NSMutableArray<myObjectCustomClass>
but no luck. Any ideas?
Upvotes: 1
Views: 5629
Reputation: 12023
You have not initialised your myObjectCustomClass while declaring as a NSMutableArray that's why its showing the error. You can initialise the array at the time of declaring or can later add elements to the array as its declared var
try initialising when you declare
var myMutableArray: NSMutableArray = [myObjectCustomClass()]
or
var myMutableArray: [myObjectCustomClass] = [myObjectCustomClass()] //swift 3 way
or alternatively declare as a CustomClass Array variable and later add objects
var myMutableArray: [myObjectCustomClass] = []
let myObjectCustomClass = myObjectCustomClass()
myMutableArray.append(myObjectCustomClass)
Upvotes: 2