I'm trying to iterate over an instance of NSOrderedSet. Something like this:
func myFunc() {
var orderedSet = NSOrderedSet(array: [ 42, 43, 44])
for n in orderedSet {
NSLog("%i", n)
}
}
...however the for loop line produces this compiler error:
'NSOrderedSet' does not have a member named 'Generator'
Now I could convert it to an array like this:
for n in orderedSet.array {
NSLog("%i", n)
}
...but I wondered if there was a better solution?
I'm also keen to understand why it's possible to iterate over a set but not an ordered set? NSOrderedSet
implements NSFastEnumeration
, so it should work right?
You can iterate over an ordered set with
let orderedSet = NSOrderedSet(array: [ 42, 43, 44])
orderedSet.enumerateObjectsUsingBlock { (elem, idx, stop) -> Void in
println("\(idx): \(elem)")
}
UPDATE: As of Swift 1.2 (Xcode 6.3), NSOrderedSet
conforms to
SequenceType
and can be enumerated with for ... in ...
:
let orderedSet = NSOrderedSet(array: [ 42, 43, 44])
for elem in orderedSet {
println(elem)
}