iosobjective-carraysswiftnsmutablearray

Error: Can't assign Value of type '[SomeObject]' to type '[NSMutableArray]'


I have been trying to assign an Swift type of array to Objective-C type of array because i'm trying to use both Objective-C and Swift Class in my project. In my swift class, I have declared an array var musicEntities = [MusicEntity]() . When i try to assign to implement the following code:

let musicVC: MusicViewController = MusicViewController.sharedInstance()
musicVC.musicEntities = musicEntities // line2 

In Line 2 i get the following error:

Can't assign Value of type '[SomeObject]' to type '[NSMutableArray]'

I know casting can help me to solve the problem.I have tried to find out specific answer in Stack Overflow but i didn't get any specific answer which might solve my problem. I tried many ways to cast it to NSMutableCompatible but i get different kind of error when i try to cast.

How can i cast that [MusicEntity] array to NSMutableArray? I need specific answer regarding my problem.

Looking forward for the answer.Thanks.


Solution

  • Try calling mutableCopy on musicEntities after casting it as NSArray and then downcast the result of that to NSMutableArray:

    musicVC.musicEntities = (musicEntities as NSArray).mutableCopy() as! NSMutableArray