1

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.?

Amila Prasad
  • 193
  • 1
  • 8

3 Answers3

7

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. :(

Amila Prasad
  • 193
  • 1
  • 8
  • 3
    `NSMutableArray(array: MyAnyObjectArray)` is probably nicer, but be aware that with your code or my code if the objective C actually mutates the array then those mutations won't be reflected in `MyAnyObjectArray` – Paulw11 Nov 02 '17 at 04:42
1

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

NSMutableArray(array: anyObjectArray)
Renetik
  • 5,887
  • 1
  • 47
  • 66
0

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"