Swift #Predicate 宏条件

问题描述 投票:0回答:1

我无法将 #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>'
ios swift swift-data
1个回答
0
投票

您无法访问对象上的属性,

point.lat
,在谓词内部,您需要在外部执行此操作

let latitude = point.lat
let predicate: Predicate<ElevationPendingList> = #Predicate {
    $0.lat == latitude
}
© www.soinside.com 2019 - 2024. All rights reserved.