Sequence
This commit is contained in:
@@ -3,7 +3,10 @@ import Units
|
|||||||
|
|
||||||
@dynamicMemberLookup
|
@dynamicMemberLookup
|
||||||
public struct Nutriments: Codable, ObjectDebugger {
|
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() {}
|
public init() {}
|
||||||
|
|
||||||
@@ -254,4 +257,22 @@ extension Nutriments {
|
|||||||
public var saturatedFat: Nutrient? { nutrients["saturated_fat"] }
|
public var saturatedFat: Nutrient? { nutrients["saturated_fat"] }
|
||||||
public var sodium: Nutrient? { nutrients["sodium"] }
|
public var sodium: Nutrient? { nutrients["sodium"] }
|
||||||
public var sugars: Nutrient? { nutrients["sugars"] }
|
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)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user