gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 05:45:57 26 4
gpt4 key购买 nike

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

    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:]:此类与键 appointmentDate 的键值编码不兼容。'

排序函数调用

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

最佳答案

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

你可以让它成为非可选的:var appointmentDate:Double,使用一个 NSNumber 对象:var appointmentDate:NSNumber!,或者使用一个 Swift 数组和内置的-在 sorted 函数中。

关于ios - [ valueForUndefinedKey :]: this class is not key value coding-compliant for the key appointmentDate. '-swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29814783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com