From 06f78e8233fd7c4e7d7817d0b87348af6e0628cd Mon Sep 17 00:00:00 2001 From: cmako10 Date: Mon, 13 Jun 2016 10:56:49 -0700 Subject: [PATCH] Create SequenceTypeConformance.swift Thanks a ton for this awesome code! I used this to so that an `OrderedDictionary` could be iterated through in a for in loop. I thought that it may improve the capabilities of this awesome code. --- SequenceTypeConformance.swift | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 SequenceTypeConformance.swift diff --git a/SequenceTypeConformance.swift b/SequenceTypeConformance.swift new file mode 100644 index 0000000..89ab107 --- /dev/null +++ b/SequenceTypeConformance.swift @@ -0,0 +1,27 @@ +extension OrderedDictionary : SequenceType { + func generate() -> OrderedDictionaryGenerator { + return OrderedDictionaryGenerator(orderedDictionary: self) + } +} + +struct OrderedDictionaryGenerator: GeneratorType { + + var orderedDictionary: OrderedDictionary + var index = 0 + + init(orderedDictionary: OrderedDictionary) { + self.orderedDictionary = orderedDictionary + } + + mutating func next() -> (key, value)? { + if index < orderedDictionary.count { + let key = orderedDictionary.keys[index] + let value = orderedDictionary[index] + index += 1 + return(key, value!) + } else { + index = 0 + return nil + } + } +}