Dattatray Deokar
Dattatray Deokar

Reputation: 2113

[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key appointmentDate.' - swift

i want to sort an array of object below is my function to sort the array of object

    class func Fn_SortByParameter(arrayToSort:NSMutableArray,paramName:NSString!, isAscending:Bool!){
    var sortDescriptor:NSSortDescriptor = NSSortDescriptor(key: paramName, ascending: isAscending, selector: Selector("localizedCompare:"))
    var sortDescriptors:NSArray = NSArray(object: sortDescriptor)
    var sortedArray:NSArray = arrayToSort.sortedArrayUsingDescriptors(sortDescriptors)
    arrayToSort.removeAllObjects()
    arrayToSort.addObjectsFromArray(sortedArray)
}

AND

class func Fn_SortByParameter(arrayToSort:NSMutableArray,paramName:NSString!, isAscending:Bool!){
    var sortDescriptor:NSSortDescriptor = NSSortDescriptor(key: paramName, ascending: isAscending)
    var sortDescriptors:NSArray = NSArray(object: sortDescriptor)
    var sortedArray:NSArray = arrayToSort.sortedArrayUsingDescriptors(sortDescriptors)
    arrayToSort.removeAllObjects()
    arrayToSort.addObjectsFromArray(sortedArray)
}

array contains objects of below class

class Appointment: NSObject {

     var id:Double!
      var status:NSString!
      var clinic:Clinic!
      var medicalCase:MedicalCase!
      var patient:Patient!
      var appointmentDate:Double! // Unix timestamp
      var reasonForVisit:NSString!
      var cancellationReason:NSString!
      var visit:Visit!
    }

When i am trying to sort it is crashing with below error

[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key appointmentDate.'

Function call to sort

Fn_SortByParameter(allAppointments.aaData, paramName: "appointmentDate", isAscending: true)

Upvotes: 3

Views: 3510

Answers (2)

SARATH SASI
SARATH SASI

Reputation: 1415

Such inexplicable exceptions are often a result of an unclean xib or Storyboard file. Open the xib or Storyboard in xcode, select File's Owner and click on the "Connection Inspector" (upper right arrow), to see all outlets at once. Look for !s which indicates a missing outlet.

in detail

1) Go to class xib file or if its in storyboard

2) Right click on UITableView, remove all earlier bindings

3) Add new binding by providing IBOutlet, delegate and datasource.

4) Clean project and run again.

Upvotes: 0

Brian Nickel
Brian Nickel

Reputation: 27560

The problem you're running into is that optional value types like Double! are not exposed to the objective-c runtime and not available for key-value coding.

You can make it non-optional: var appointmentDate:Double, use an NSNumber object: var appointmentDate:NSNumber!, or use a Swift array and the built-in sorted function.

Upvotes: 15

Related Questions