首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CoreData NSPredicate

CoreData NSPredicate
EN

Stack Overflow用户
提问于 2018-09-25 16:52:48
回答 3查看 228关注 0票数 0

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

现在,当试图执行此代码时,编译器会在下面抛出错误:

代码语言:javascript
复制
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类型的未明确例外结束

任何指针都将不胜感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-10-10 10:51:47

最后,我将检查类型更改为NSSet,并显式转换筛选(using:_) ->的结果,更改代码如下:

代码语言:javascript
复制
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))"
}

谢谢你的评论,他们为我指明了正确的方向!

票数 0
EN

Stack Overflow用户

发布于 2018-09-28 12:59:16

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

代码假定有一个NSManagedObject子类Inspection具有关系item,由item表示的实体具有唯一的属性name

代码语言:javascript
复制
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。

代码语言:javascript
复制
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) }
票数 1
EN

Stack Overflow用户

发布于 2018-09-28 11:39:38

尝试避免实体属性的显式字符串,还可以用正确的格式表达式替换false

代码语言:javascript
复制
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))"
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52503233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档