This commit is contained in:
cdricms
2025-09-13 13:55:31 +02:00
parent 9f8c9c7853
commit 4b1a4629fb

View File

@@ -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<String, Nutrient>.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)
}