I am trying to write an extension where I define a new method for sorting the contained objects in-place by their property using NSSortDescriptorclass. However, I failed to populate the mutable version of self for some reason and getting the following error:
Cannot find an initializer for type NSMutableArray that accepts an argument list of type (array: Array<T>)
Here is my method:
extension Array {
public mutating func sortInPlace(field: String, ascending: Bool) {
let mutableArray = NSMutableArray(array: self) // error here.
let sortDescriptor = NSSortDescriptor(key: field, ascending: ascending)
mutableArray.sortUsingDescriptors([sortDescriptor])
self = mutableArray as AnyObject as! [Generator.Element]
}
}
I also tried the following but no luck:
let mutableArray = NSMutableArray(array: self as! [AnyObject])