Swift3: not the expected contextual result type NSArray

633 views Asked by At

I am migrating my code from Swift 2.3 to Swift 3. Before it was working fine after migrating I am facing not expected contextual result type NSArray Here is my code

func setConfirmedBookingsAfterSorting() {
        if let bookings =  ContentService.sharedInstance.confirmedBookings {
            self.confirmedBookings = (bookings as NSArray).sortedArray(using: [NSSortDescriptor(key: "startTime", ascending: true)])
        }
    }
1

There are 1 answers

3
breno morais On

The declared type of the method used is func sortedArray(using sortDescriptors: [NSSortDescriptor]) -> [Any], which means the result is implicitly converted to a Swift array.

Try this:

((bookings as NSArray).sortedArray(using: [NSSortDescriptor(key: "startTime", ascending: true)])) as NSArray

This prevents the conversion and maintains the result as an NSArray Objective C object.