ios - [ valueForUndefinedKey:]: this class is not key value coding-compliant for the key appointmentDate.' - swift -
i want sort array of object below function sort 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 trying sort crashing below error
[ valueforundefinedkey:]: class not key value coding-compliant key appointmentdate.'
function call sort
fn_sortbyparameter(allappointments.aadata, paramname: "appointmentdate", isascending: true)
the problem you're running optional value types double!
not exposed objective-c runtime , not available key-value coding.
you can make non-optional: var appointmentdate:double
, use nsnumber object: var appointmentdate:nsnumber!
, or use swift array , built-in sorted
function.