我无法将 #Predicate Macro 用于简单条件。
for point in pendingList {
do {
let predicate: Predicate<ElevationPendingList> = #Predicate {
$0.lat == point.lat //It throws error, I will attach error at bottom.
}
// Create a FetchDescriptor with the predicate
let descriptor = FetchDescriptor<Item>(predicate: predicate)
if let matchedPoint = try modelContext?.fetch(descriptor).first {
modelContext?.delete(matchedPoint)
}
} catch {
print("Error saving model object:", error)
}
}
错误:-
Cannot convert value of type 'PredicateExpressions.Equal<PredicateExpressions.KeyPath<PredicateExpressions.Variable<ElevationPendingList>, Double>, PredicateExpressions.KeyPath<PredicateExpressions.Value<ElevationDataModel>, Double>>' to closure result type 'any StandardPredicateExpression<Bool>'
您无法访问对象上的属性,
point.lat
,在谓词内部,您需要在外部执行此操作
let latitude = point.lat
let predicate: Predicate<ElevationPendingList> = #Predicate {
$0.lat == latitude
}