将嵌套的条件 If 语句转换为复杂的 NSCompoundPredicate

Translate a nested Conditional If Statement into a complex NSCompoundPredicate

我正在使用 SwiftUI 为基于 iOS 的旧项目编写 macOS 目标。是一个核心数据驱动的应用程序,对于 macOS 目标,我已经成功地使用动态 @FetchRequest 实现了通用列表,主要如 Paul Hudson 在他的 blog.

中所描述的那样

我主要通过遵循 Apple SwiftUI Tutorials 并复制提供的示例代码来构建目标。

之前使用的条件if语句根据3个UI控件主动过滤每个SwiftUIList

// PART 1
    if (!self.appData.showFavouritesOnly
        || fetchedEvent.isFavourite)
// PART 2
    && (self.searchText.count == 0
        || (fetchedEvent.eventName?.contains(self.searchText) == true))
// PART 3
    && (self.filter == .all
        || self.filter.name == fetchedEvent.eventCategory
        || (self.filter.category == .featured && fetchedEvent.isFeatured)) {

现在我有一个使用谓词的通用@FetchRequest,我想将这个条件 if 语句转换为 NSCompoundPredicate

我将包含整个初始化程序,以便您了解动态 @FetchRequest 是如何构建的,但它是我需要协助的谓词...

init(sortDescriptors: [NSSortDescriptor],
     searchKey: String,
     searchValue: String?,
     showFavourites: Bool,
     filterKey: String,
     filter: FilterType,
     @ViewBuilder content: @escaping (T) -> Content) {

    let entity = T.entity
    let predicateTrue = NSPredicate(value: true)
// PART 1
    let predicateFavourite = showFavourites == false ? predicateTrue : NSPredicate(format: "isFavourite == TRUE")
// PART 2
    let predicateSearch = searchValue?.count == 0 ? predicateTrue : NSPredicate(format: "%K CONTAINS[cd] %@", searchKey, searchValue!)

    // The initialiser works perfectly down to this point...then...
// PART 3
    let predicateFilterName = filter == .all ? predicateTrue : NSPredicate(format: "%K == %@", filterKey, filter.name as CVarArg)
    let predicateFilterFeature = filter.category == .featured ? NSPredicate(format: "isFeatured == TRUE") : predicateTrue

    let predicateOr = NSCompoundPredicate(orPredicateWithSubpredicates: [predicateFilterName, predicateFilterFeature])
    let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicateFavourite, predicateSearch, predicateOr])

    fetchRequest =
        FetchRequest<T>(entity: entity,
                        sortDescriptors: sortDescriptors,
                        predicate: predicate)

    self.content = content
}

此处包含在第 3 部分下的代码部分有效。在 FilterType.allFilterType.featured 之间切换会产生预期的变化,但是我正在努力为选择另一个类别的 "other" 情况编写谓词 - 即 - 不是特色,但 EITHER .lakes.rivers.mountains

为了完整性,我还包含了枚举 Category 和结构 FilterType...

enum Category: String, CaseIterable, Codable, Hashable {

    case featured = "Featured"
    case lakes = "Lakes"
    case rivers = "Rivers"
    case mountains = "Mountains"
}

struct FilterType: CaseIterable, Hashable, Identifiable {

    var name: String
    var category: Category?

    init(_ category: Category) {
        self.name = category.rawValue
        self.category = category
    }

    init(name: String) {
        self.name = name
        self.category = nil
    }

    static var all = FilterType(name: "All")

    static var allCases: [FilterType] {
        return [.all] + Category.allCases.map(FilterType.init)
    }

    var id: FilterType {
        return self
    }

}

我认为问题出在这里:

 let predicateFilterFeature = filter.category == .featured ? NSPredicate(format: "isFeatured == TRUE") : predicateTrue

如果过滤器是 .lakes 等,则此子谓词将为 TRUE,当与 predicateFilterName 进行或运算时会覆盖它。尝试返回 FALSE:

 let predicateFilterFeature = filter.category == .featured ? NSPredicate(format: "isFeatured == TRUE") : predicateFalse

我的答案是欺诈性的,因为它是我在尝试以不同方式思考谓词的构造后偶然发现的黑客解决方案。 (它没有表现出对理解我编写的谓词语法的实际问题的任何仔细思考!)

所以这个回复没有回答我原来的问题——@pbasdf 已经回答了——尽管这个回复达到了同样的结果。

那么也许是替代解决方案?!?

let predicateTrue = NSPredicate(value: true)

let predicateFavourite = showFavourites == false ? predicateTrue : NSPredicate(format: "isFavourite == TRUE")

let predicateSearch = searchValue?.count == 0 ? predicateTrue : NSPredicate(format: "%K CONTAINS[cd] %@", searchKey, searchValue!)

let predicateFilterFeatured = NSPredicate(format: "isFeatured == TRUE")
let predicateFilterName = NSPredicate(format: "%K == %@", filterKey, filter.name as CVarArg)
let predicateFilterCategory = filter.category == .featured ? predicateFilterFeatured : predicateFilterName

let predicateFilter = filter == .all ? predicateTrue : predicateFilterCategory

let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicateFavourite, predicateSearch, predicateFilter])