Swift 5.9 和新的 SwiftData 框架引入了@Model和#Predicate宏。我们现在可以在模型中使用自定义枚举和结构,如下所示:
@Model
final class Item {
var name: Name
var nature: Nature
struct Name: Codable {
let base: String
let suffix: String?
}
enum Nature: Int, Codable {
case foo = 0
case bar = 1
case baz = 2
}
init(name: Name, nature: Nature) {
self.name = name
self.nature = nature
}
}
Run Code Online (Sandbox Code Playgroud)
但是我们如何在 a 中使用它们呢Predicate?所有这些例子都失败了:
// Member access without an explicit base is not allowed in this predicate (from macro 'Predicate')
let predicate = #Predicate<Item> { $0.nature == .foo }
// Cannot infer key path type from context; consider explicitly specifying a root type.
let predicate = #Predicate<Item> { $0.nature.rawValue == Item.Nature.foo.rawValue }
// No compile error, but causes the Xcode Preview to crash when used inside a @Query.
let predicate = #Predicate<Item> { $0.name.base.contains("Hello, world!") }
// Optional chaining is not supported here in this predicate. Use the flatMap(_:) function explicitly instead. (from macro 'Predicate').
let predicate = #Predicate<Item> { $0.name.suffix?.contains("Hello, world!") ?? false }
Run Code Online (Sandbox Code Playgroud)
// 此谓词不支持可选链接。显式地使用 flatMap(_:) 函数来代替。(来自宏“谓词”)。
let predicate = #Predicate<Item> { $0.name.suffix?.contains("Hello, world!") ?? false }
-->
let predicate = #Predicate<Item> { $0.name.suffix.flatMap { $0.contains("Hello, world!") } == true }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1513 次 |
| 最近记录: |