如何使用键(字符串)和结果(nsfetchrequestresult)定义pectractedfetchresults的扩展
我的 plocteredfetchresults 按这样的定义:
private var consumption: SectionedFetchResults<String?, MedicationConsumption>
药物消费的数量可变。我需要写扩展,以进行消费,它将具有功能 totalQuantity 。我尝试过各种方法来扩展它,但没有任何运气。
如何在 phoctereDfetchResults上写扩展名,它将返回 totalQuantity ??
不起作用的第1个扩展:
extension SectionedFetchResults where Result == NSManagedObject {
var totalQuantity: Int { /* Code here */ }
}
不编译的第二个扩展名
extension SectionedFetchResults<String, NSFetchRequestResult>.Element {
var totalQuantity: Int { /* Code here */ }
}
:代码:
static var fetchRequest: NSFetchRequest<MedicationConsumption> {
let result = MedicationConsumption.fetchRequest()
result.sortDescriptors = [NSSortDescriptor(keyPath: \MedicationConsumption.date, ascending: true)]
result.predicate = NSPredicate(format: "(medication.type == 1)")
return result
}
@SectionedFetchRequest(fetchRequest: Self.fetchRequest, sectionIdentifier: \MedicationConsumption.group)
private var consumption: SectionedFetchResults<String?, MedicationConsumption>
// Use case scenario
var body: some View {
List(waterConsumption) { section in
VStack {
Text("\(section.totalQuantity)")
}
}
}
药物消费:nsmanagedObject
extension MedicationConsumption {
@nonobjc public class func fetchRequest() -> NSFetchRequest<MedicationConsumption> {
return NSFetchRequest<MedicationConsumption>(entityName: "MedicationConsumption")
}
@NSManaged public var date: Date?
@NSManaged public var group: String?
@NSManaged public var quantity: Double
@NSManaged public var medication: Medication?
}
I have SectionedFetchResults defined like this:
private var consumption: SectionedFetchResults<String?, MedicationConsumption>
MedicationConsumption have variable quantity. I need to write extension for consumption that, it will have function totalQuantity. I have tried various way on extending it, but without any luck.
How to write extension on SectionedFetchResults, that it will return totalQuantity??
1st extension which is not working:
extension SectionedFetchResults where Result == NSManagedObject {
var totalQuantity: Int { /* Code here */ }
}
2nd extension which is not compiling:
extension SectionedFetchResults<String, NSFetchRequestResult>.Element {
var totalQuantity: Int { /* Code here */ }
}
Source code:
static var fetchRequest: NSFetchRequest<MedicationConsumption> {
let result = MedicationConsumption.fetchRequest()
result.sortDescriptors = [NSSortDescriptor(keyPath: \MedicationConsumption.date, ascending: true)]
result.predicate = NSPredicate(format: "(medication.type == 1)")
return result
}
@SectionedFetchRequest(fetchRequest: Self.fetchRequest, sectionIdentifier: \MedicationConsumption.group)
private var consumption: SectionedFetchResults<String?, MedicationConsumption>
// Use case scenario
var body: some View {
List(waterConsumption) { section in
VStack {
Text("\(section.totalQuantity)")
}
}
}
MedicationConsumption: NSManagedObject
extension MedicationConsumption {
@nonobjc public class func fetchRequest() -> NSFetchRequest<MedicationConsumption> {
return NSFetchRequest<MedicationConsumption>(entityName: "MedicationConsumption")
}
@NSManaged public var date: Date?
@NSManaged public var group: String?
@NSManaged public var quantity: Double
@NSManaged public var medication: Medication?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可能就像
项目中的测试模块
I assume it can be like
Tested module in project