[valueForUndefinedKey:]:此类与键约会日期的键值编码不兼容. -迅速 [英] [ 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)
}
数组包含以下类的对象
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:]:此类与键约会日期的键值编码不兼容.'
[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key appointmentDate.'
函数调用进行排序
Fn_SortByParameter(allAppointments.aaData, paramName: "appointmentDate", isAscending: true)
推荐答案
您遇到的问题是,像Double!
这样的可选值类型没有暴露给Objective-C运行时,也不可用于键值编码
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.
您可以将其设置为非可选:var appointmentDate:Double
,使用NSNumber对象:var appointmentDate:NSNumber!
,或使用Swift数组和内置的sorted
函数.
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.
这篇关于[valueForUndefinedKey:]:此类与键约会日期的键值编码不兼容. -迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!