From 4b1a4629fbdcc91958814a337136bbd5534cda2c Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Sat, 13 Sep 2025 13:55:31 +0200 Subject: [PATCH] Sequence --- .../OpenFoodFacts/Schemas/Nutriments.swift | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Sources/OpenFoodFacts/Schemas/Nutriments.swift b/Sources/OpenFoodFacts/Schemas/Nutriments.swift index 4038bd7..f8717fb 100644 --- a/Sources/OpenFoodFacts/Schemas/Nutriments.swift +++ b/Sources/OpenFoodFacts/Schemas/Nutriments.swift @@ -3,7 +3,10 @@ import Units @dynamicMemberLookup public struct Nutriments: Codable, ObjectDebugger { - private var nutrients: [String: Nutrient] = [:] // Key is accessName ( - replaced with _ ) + private var nutrients: [String: Nutrient] = [:] + + private var iterator: Dictionary.Iterator? = nil + private var keys: [String] = [] public init() {} @@ -254,4 +257,22 @@ extension Nutriments { public var saturatedFat: Nutrient? { nutrients["saturated_fat"] } public var sodium: Nutrient? { nutrients["sodium"] } public var sugars: Nutrient? { nutrients["sugars"] } + public var calcium: Nutrient? { nutrients["calcium"] } +} + +extension Nutriments: Sequence, IteratorProtocol { + public mutating func next() -> Element? { + if iterator == nil { + iterator = nutrients.makeIterator() + keys = Array(nutrients.keys).sorted() + } + guard let nextElement = iterator?.next() else { + iterator = nil + return nil + } + return (item: nextElement.key, nutrient: nextElement.value) + } + + public typealias Element = (item: String, nutrient: Nutrient) + }