0

我将一个旧项目从 Swift 1 转换为使用 CoreData 的 Swift 4.2(在主要版本步骤中,全部成功构建)。

现在编译器在尝试执行此代码时会抛出以下错误:

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 AppName[538:144354]-[_NSFaultingMutableSet filteredOrderedSetUsingPredicate:]:无法识别的选择器发送到实例 0x17063afc0 2018-09-25 17:19:06.607861+0204 * AppName:1453] ** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[_NSFaultingMutableSet filteredOrderedSetUsingPredicate:]: unrecognized selector sent to instance 0x17063afc0”

*** First throw call stack: (0x18f5d6fd8 0x18e038538 0x18f5ddef4 0x18f5daf4c 0x18f4d6d2c 0x100126ce0 0x10015fa10 0x10015fc18 0x195ff9268 0x19575f884 0x19575a4ac 0x1956fc158 0x1928ec274 0x1928e0de8 0x1928e0ca8 0x19285c360 0x1928833c0 0x1956f17a0 0x18f5849a0 0x18f582628 0x18f582a74 0x18f4b2d94 0x190f1c074 0x195764130 0x100110cb4 0x18e4c159c)

libc++abi.dylib:以 NSException 类型的未捕获异常终止

任何指针将不胜感激。

4

3 回答 3

1

强烈建议使用适当的谓词和排序描述符重新获取项目,而不是手动进行。

Inspection该代码假定有一个具有关系的 NSManagedObject 子类,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-28T12:59:16.927 回答
0

我最终将检查类型更改为 NSSet 并显式转换过滤的结果(使用:_)-> 更改代码如下:

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-10-10T10:51:47.243 回答
0

尽量避免实体属性的显式字符串,也可以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))"
}
于 2018-09-28T11:39:38.977 回答