【问题标题】:[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key appointmentDate.' - swift[ valueForUndefinedKey:]:此类不符合键约会日期的键值编码。 - 迅速
【发布时间】:2015-07-01 03:03:21
【问题描述】:

我想对一个对象数组进行排序下面是我对对象数组进行排序的函数

    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)
}

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!
    }

当我尝试排序时,它会因以下错误而崩溃

[valueForUndefinedKey:]:这个类不符合键约会日期的键值编码。'

函数调用排序

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

【问题讨论】:

    标签: ios swift sorting


    【解决方案1】:

    您遇到的问题是像Double! 这样的可选值类型没有暴露给objective-c 运行时,也不能用于键值编码。

    您可以将其设为非可选:var appointmentDate:Double,使用 NSNumber 对象:var appointmentDate:NSNumber!,或使用 Swift 数组和内置的 sorted 函数。

    【讨论】:

      【解决方案2】:

      此类莫名其妙的异常通常是由不干净的 xib 或 Storyboard 文件造成的。在 xcode 中打开 xib 或 Storyboard,选择 File's Owner 并单击“Connection Inspector”(右上箭头),一次查看所有出口。查找 表示缺少出口的 !s。

      详细

      1) 转到类 xib 文件,或者如果它在故事板中

      2) 右击UITableView,移除所有之前的绑定

      3) 通过提供 IBOutlet、delegate 和 datasource 添加新的绑定。

      4) 清理项目并再次运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-23
        • 2017-10-15
        • 1970-01-01
        • 2019-10-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多