我将一个旧项目从Swift 1转换为SWIFT4.2(在主要版本步骤中,都是成功构建的),使用CoreData。
现在,当试图执行此代码时,编译器会在下面抛出错误:
let sortDescriptor = NSSortDescriptor(key: "dateModified", ascending: false)
let predicate = NSPredicate(format: "markAsDeleted == false")
if let itemInspections = item.inspections?.filtered(using: predicate).sortedArray(using: [sortDescriptor]) {
label.text = "Items (\(itemInspections.count))"
}错误:
2018-09-25 17:19:06.606722+0200 AppName538:144354 -_NSFaultingMutableSet filteredOrderedSetUsingPredicate::未识别的选择器发送给实例0x17063afc0 2018-09-25 17:19:06.607861+0200 AppName538:144354 *由于不明异常'NSInvalidArgumentException',原因:‘_NSFaultingMutableSet filteredOrderedSetUsingPredicate::未识别的选择器发送到实例0x17063afc0’ *第一次抛出调用堆栈:(0x18f5d6fd8 0x1818e038538 0x18f5ddef4 0x18f5df5df4c 0x18f4d6d2c 0x100126ce0 0x10015fa10 0x10015fa10 0x10015fc18 0x195ff9268 0x19575f884 0x19575a4ac 0x1956fc158 0x1928ec274 0x1928e0de8 0x28e0ca8 0285c360 0x0x1956f6f17a0 18f4958a0 0x18f4958a0 0x18f492828 0x18f774 0fb4d2x74x11x1x1918c0c0 0 288c360 0x1956c0 0x1956f17a0 18f4958a0 0x18f5828 0x18f774) libc++abi.dylib:以NSException类型的未明确例外结束
任何指针都将不胜感激。
发布于 2018-10-10 10:51:47
最后,我将检查类型更改为NSSet,并显式转换筛选(using:_) ->的结果,更改代码如下:
let sortDescriptor = NSSortDescriptor(key: "dateModified", ascending: false)
let predicate = NSPredicate(format: "markAsDeleted == false")
if let itemInspections = NSSet(set: (item.inspections?.filtered(using: predicate))!).sortedArray(using: [sortDescriptor]) as? [Inspection] {
inspectionsCountLabel.text = "Inspections (\(itemInspections.count))"
}谢谢你的评论,他们为我指明了正确的方向!
发布于 2018-09-28 12:59:16
强烈建议使用适当的谓词和排序描述符重新获取项,而不是手动进行。
代码假定有一个NSManagedObject子类Inspection具有关系item,由item表示的实体具有唯一的属性name。
let sortDescriptor = NSSortDescriptor(key: "dateModified", ascending: false)
let predicate = NSPredicate(format: "item.name == %@ AND markAsDeleted == FALSE", item.name)
let fetchRequest : NSFetchRequest<Inspection> = Inspection.fetchRequest()
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = [sortDescriptor]
do {
let itemInspections = try managedObjectContext.fetch(fetchRequest)
label.text = "Items (\(itemInspections.count))"
} catch { print(error) }如果您只需要过滤项的数量,则有一个更有效的API。
let sortDescriptor = NSSortDescriptor(key: "dateModified", ascending: false)
let predicate = NSPredicate(format: "item.name == %@ AND markAsDeleted == FALSE", item.name)
let fetchRequest : NSFetchRequest<Inspection> = Inspection.fetchRequest()
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = [sortDescriptor]
do {
let numberOfItemInspections = try managedObjectContext.count(for: fetchRequest)
label.text = "Items (\(numberOfItemInspections))"
} catch { print(error) }发布于 2018-09-28 11:39:38
尝试避免实体属性的显式字符串,还可以用正确的格式表达式替换false:
let sortDescriptor = NSSortDescriptor(key: "\#keyPath(YourClassHere.dateModified)", ascending: false)
let predicate = NSPredicate(format: "\#keyPath(YourClassHere.markAsDeleted) == @%", NSNummber(value: false))
if let itemInspections = item.inspections?.filtered(using: predicate).sortedArray(using: [sortDescriptor]) {
label.text = "Items (\(itemInspections.count))"
}https://stackoverflow.com/questions/52503233
复制相似问题