Build & Test

针对 macOS 14.4 / iOS 17.4 或更高版本的开发者

Swift Foundation 的这个 PR 增加了 Predicate 的复合功能,如下所示:如果你的目标版本仅为这些版本及以上,则应使用此功能。

let notTooShort = #Predicate<Book> { $0.pages > 50 }
let notTooLong = #Predicate<Book> { $0.pages <= 350 }
let titleFilter = #Predicate<Book> { $0.title.contains("Swift") }

let filter = #Predicate<Book> {
    (notTooShort.evaluate($0) && notTooLong.evaluate($0)) || titleFilter.evaluate($0)
}

CompoundPredicate

CompoundPredicate 旨在改进 Predicate 系统,以便在构造多个谓词后能够组合它们。

let notTooShort = #Predicate<Book> {
    $0.pages > 50
}

let notTooLong = #Predicate<Book> {
    $0.pages <= 350
}

let lengthFilter = [notTooShort, notTooShort].conjunction()

// Match Books that are just the right length
let titleFilter = #Predicate<Book> {
    $0.title.contains("Swift")
}

// Match Books that contain "Swift" in the title or
// are just the right length
let filter = [lengthFilter, titleFilter].disjunction()

文档

文档可在此处获得:here,并且您可以像 Docc 归档一样使用 Xcode 查看。

反馈

请随时创建 Issue,或者更好的是,通过创建 pull request 积极贡献。

实现进度