gpt4 book ai didi

ios - CoreStore 分段列表监视器如何在运行时指定 .where 子句

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:35 25 4
gpt4 key购买 nike

我的初始化方法中有这段代码:

self.monitor = CoreStore.monitorSectionedList(
From<ListEntityType>()
.sectionBy(#keyPath(ListEntityType.muscle.name)) { (sectionName) -> String? in
return "\(String(describing: sectionName)) years old"
}
.orderBy(.ascending(#keyPath(ListEntityType.muscle.name)), .ascending(\.name))
)

我想以某种方式在运行时向此监视器添加 .where 条件。

ListEntityType 是名为 Exercise 的实体的类型别名。所以每个 Exercise 都包含一对一关系 Muscle (exercise.muscle)。

每个实体都有唯一的标识符属性。

我有一组肌肉标识符,我想将其用作过滤器以显示分段列表中的对象。

如何循环遍历此标识符以将 then to .where 子句添加到 CoreStore.monitorSectionedList

我可能期待这样的事情但不是 100% 肯定只是假设:

让 predicate = NSPredicate(格式: "%K = $ARGUMENT")

    var predicates = [NSPredicate]()

if let muscles : [MuscleGroupEntity] = workout?.muscles.toArray() {
for muscle in muscles
{
let myNewPredicate = predicate.withSubstitutionVariables(["ARGUMENT" : muscle.id])
predicates.append(myNewPredicate)
}
}

self.monitor = CoreStore.monitorSectionedList(
From<ListEntityType>()
.sectionBy(#keyPath(ListEntityType.muscle.name)) { (sectionName) -> String? in
return "\(String(describing: sectionName)) years old"
}
.where(format:"%K = %@", argumentArray: predicates)
.orderBy(.ascending(#keyPath(ListEntityType.muscle.name)), .ascending(\.name))
)

此代码因错误而崩溃:

-[NSComparisonPredicate rangeOfString:]: unrecognized selector sent to

我想这是我发现的谓词 here ,但不确定如何在我的情况下正确使用它

已编辑 MartinM 评论:

我有一个带有属性 Muscle 的 ExerciseEntity。

如果我想只使用一个肌肉 id 进行所有练习,那非常简单:

.where(format:"%K = %@", #keyPath(ExerciseEntity.muscle.id), "id_12345")

但我想指定更多的肌肉并在运行时定义它们。不知何故,我需要了解格式是什么、参数是什么以及如何传递标识符数组而不是一个 "id_12345"

最佳答案

如果您想获取所有 ExerciseEntity,其中 muscle.id 包含在 muscleId 列表中,您只需使用:

let muscleIds = workout?.muscles.compactMap({ $0.id }) ?? []
From....
.where(Where<ExerciseEntity>(#keyPath(ExerciseEntity.muscle.id), isMemberOf: muscleIds))

这相当于一个谓词,格式为:%K IN %@, ExerciseEntity.muscle.id, muscleIds

这也适用于取反 -> NOT(%K IN %@),就像您可以在 CoreStore 中使用 !Where 一样。

复合谓词需要指定它们的链接方式。 CoreStore 一次支持 2 个谓词的简写,使用逻辑运算符,例如:

Where<ExerciseEntity>(yourWhereClause) && Where<ExerciseEntity(otherWhereClause)

这相当于像这样使用 NSCompoundPredicate:

NSCompoundPredicate(type: .and, subpredicates: [left.predicate, right.predicate])

如果您需要更复杂的连词,您也可以将该复合谓词作为参数直接传递给 where 子句。然后,您还可以将该复合谓词存储在某处,并附加另一个符合您需要的复合谓词。

关于ios - CoreStore 分段列表监视器如何在运行时指定 .where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57731824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com