如何使用键(字符串)和结果(nsfetchrequestresult)定义pectractedfetchresults的扩展

发布于 2025-01-31 18:16:36 字数 1896 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双手揣兜 2025-02-07 18:16:36

我认为这可能就像

  1. 是总的响应:
extension SectionedFetchResults where Result == MedicationConsumption {
    var totalQuantity: Double {
        self.reduce(0) { sum, section in
            section.reduce(into: sum) { $0 += $1.quantity }
        }
    }
}
  1. 对于总计
extension SectionedFetchResults.Section where Result == MedicationConsumption {
    var totalQuantity: Double {
        self.reduce(0) { $0 + $1.quantity }
    }
}

项目中的测试模块

I assume it can be like

  1. for Grand Total for response:
extension SectionedFetchResults where Result == MedicationConsumption {
    var totalQuantity: Double {
        self.reduce(0) { sum, section in
            section.reduce(into: sum) { $0 += $1.quantity }
        }
    }
}
  1. for total in Section
extension SectionedFetchResults.Section where Result == MedicationConsumption {
    var totalQuantity: Double {
        self.reduce(0) { $0 + $1.quantity }
    }
}

Tested module in project

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文