- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的初始化方法中有这段代码:
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/
我在 Swift 5 中使用 CoreData 的 CoreStore 包装器 import CoreStore @objc(Post) public class Post: NSManagedObj
我要解决下一个问题: 我想在上下文中使用一些 NSManagedObject 并在运行时更改一些属性,但不告诉 SQLite 其中的任何更改。 我只想在点击保存按钮或类似按钮时将 NSManagedO
我使用这段代码来创建一个对象: CoreStore.perform( asynchronous: { (transaction) -> Void in
我有下一个代码来更新我的锻炼: func addMuscleGroups(muscleGroups: [MusclEntity], toWorkout: WorkoutEntity, completi
我有 NSManagedObject 数据模型 ExerciseEntity 我也有数据模式 MuscleEntity。我可以通过 exercise.muscle 访问肌肉对象 我添加了 CoreSt
我找到了我认为不错的图书馆 CoreStore正如我们所见,它们提供了导入和唯一导入以及可导入协议(protocol)背后的映射功能,我们需要在 NSManagedObject 对象子类中实现这些功能
我想通过删除关系中的一些对象来更新我的核心数据对象。 我知道 CoreStore 提供了很少的更新和删除方法。我的问题是我可以使用更新功能删除吗? let john: MyPersonEntity .
我的初始化方法中有这段代码: self.monitor = CoreStore.monitorSectionedList( From() .sectionBy(
我想使用 UITableview 实现分页类型的用户界面,以便一旦用户在滚动时最后记录,我想使用 CoreStore 从本地数据库中获取下一组记录。 是否CoreStore查询时支持这种功能吗? 最佳
在我使用 CoreStore 5.3 的应用程序中,我试图将我的本地 sqlite 存储移动到另一个位置,以便能够从应用程序扩展访问数据库。使用 FileManager.default.moveIte
我从文档中找到这段代码: let jane: MyPersonEntity = // ... CoreStore.perform( asynchronous: { (transaction)
如果我弄清楚了,我需要在搜索某些内容时重新获取监视器: 我有这个函数来重新获取提供的字符串 func search(searchText: String) { self.monitor
我是一名优秀的程序员,十分优秀!