【问题标题】:NSPredicate to return all except given objectNSPredicate 返回除给定对象之外的所有对象
【发布时间】:2020-01-06 22:35:10
【问题描述】:

找不到允许我获取所有对象的 NSPredicate 语法,除了一个给定的对象

  fetchRequest.predicate = NSPredicate(format: "SELF != %@", setting.objectID)

我的实体没有任何存储的 ID。在 cmets 的某处发现了该语法,但它不起作用:

Generic parameter 'Subject' could not be inferred

任何想法如何过滤除一个给定对象之外的所有对象,而不过滤多个属性?

这是我的观点(使用 SwiftUI):

struct SettingsChoiseView: View {
    @Binding var actualSetting: Settings
    @Environment(\.managedObjectContext) var moc: NSManagedObjectContext
    @FetchRequest var avalibleSettings: FetchedResults<Settings>

     init(setting: Binding<Settings>) {
         let fetchRequest: NSFetchRequest< Settings> = Settings.fetchRequest()
         fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \ Settings.name, ascending: true),
                                    NSSortDescriptor(keyPath: \ Settings.height, ascending: true)]
         fetchRequest.predicate = NSPredicate(format: "!(name = %@ && height == %d)", setting.name.wrappedValue, setting.height.wrappedValue)

            self._avalibleSettings = FetchRequest(fetchRequest:fetchRequest)
    }
    var body: some View{
        ...
    }
}

SettingsNSManagedObject,具有属性名称和高度。

【问题讨论】:

标签: core-data swiftui nspredicate


【解决方案1】:

感谢@Asperi,我明白了:

        fetchRequest.predicate = NSPredicate(format: "NOT (self in %@)", [setting.wrappedValue])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多