我正在使用 SwiftUI 为基于 iOS 的旧项目编写 macOS 目标。是一个核心数据驱动的应用程序,对于 macOS 目标,我已经使用动态 @FetchRequest 成功实现了通用列表,主要如 Paul Hudson 在他的博客中所描述的那样。
我主要通过遵循 Apple 的SwiftUI 教程并复制提供的示例代码来构建目标。
之前使用的条件 if 语句List
根据 3 个 UI 控件主动过滤每个 SwiftUI。
// 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.all
并FilterType.featured
进行预期的更改,但是我正在努力为选择另一个类别的“其他”情况编写谓词 - 即 - NOT Featured,但是.lakes
EITHER.rivers
或.mountains
.
为了完整起见,我还包括了 enumCategory
和 struct 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
}
}