Amila Prasad
Amila Prasad

Reputation: 193

could not cast value of type 'Swift._ContiguousArrayStorage<Swift.AnyObject>' to 'NSMutableArray'

I accidently updated iOS version in my phone from 10.3.3 to 11.0.3. Because of that I had to upgrade my xcode to latest 9.1 version. Now when I run my code in xcode 9.1 it appears

could not cast value of type 'Swift._ContiguousArrayStorage' to 'NSMutableArray'

It seems to be there is no longer conversion from 'AnyObject' array to 'NSMutableArray'. But I have used this conversion throughout my code. I have spend almost a day trying using [[String : AnyObject]] instead [AnyObject] which was not a good idea as it affects throughout the code and it feels like I'm doing my code again from the scratch. Can anyone please suggest a better solution.?

Upvotes: 1

Views: 5213

Answers (3)

Renetik
Renetik

Reputation: 6373

As I see this just in comments and in my opinion should be accepted answer :

NSMutableArray(array: anyObjectArray)

Upvotes: 1

anshul-systematix
anshul-systematix

Reputation: 416

SWIFT-4.

SORTING OF NSARRAY OR MUTABLEARRAY IN SWIFT

If you have NSArray then it should be done easily. And if you have NSMutableArray, please refer this one.

    var ary_country:NSMutableArray = []
    print("Before ",ary_country) //"A", "E", "D", "B"
    let sorted = ary_country.sorted {($0 as AnyObject).localizedStandardCompare($1 as! String) == .orderedAscending}
   ary_country =  (sorted as NSArray).mutableCopy() as! NSMutableArray
   print("After", ary_country) //"A", "B", "C", "D"

Upvotes: 0

Amila Prasad
Amila Prasad

Reputation: 193

Solved it by using

(MyAnyObjectArray! as NSArray).mutableCopy() as! NSMutableArray

instead of

MyAnyObjectArray as! NSMutableArray

When I used xcode 8.3.3 and swift 3.2 use of .mutableCopy() was a error prone. But now it sees to be ok to use. Kind of confused. :(

Upvotes: 7

Related Questions