Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over an NSOrderedSet

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?

like image 422
Andrew Ebling Avatar asked Oct 28 '14 08:10

Andrew Ebling


3 Answers

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)
}
like image 161
Martin R Avatar answered Nov 14 '22 02:11

Martin R


NSOrderedSet doesn't conform to SequenceType. NSOrderedSet is subclass of NSObject and not NSSet as one could imagine. I guess Apple engineers overlooked it.

like image 42
Kirsteins Avatar answered Nov 14 '22 02:11

Kirsteins


The Swifty, simplest, and most general solution would be to shallow copy to an array in O(1) - per the docs. This would allow you to use Swift's other functional techniques and functions.

import Swift
import Foundation


println("Simplest, most accessible, O(1) performance: shallow copy to array:")

var set = NSOrderedSet(array: map(0...7) { d in d })
for d in set.array as [Int] {
    print("\t\(d)")
}

println("\n\nIn general - for other types and cases, you could create a sequence:")

extension NSOrderedSet {
    func sequenceOf<T>(t:T.Type) -> SequenceOf<T> {
        var current = 0
        return SequenceOf(GeneratorOf({ () -> T? in
            return current < self.count ? self.objectAtIndex(current++) as? T : nil
        }))
    }
}

for d in set.sequenceOf(Int.self) {
    print("\t\(d)")
}

Simplest, most accessible, O(1) performance: shallow copy to array:

0 1 2 3 4 5 6 7

In general - for other types and cases, you could create a sequence:

0 1 2 3 4 5 6 7

like image 29
Chris Conover Avatar answered Nov 14 '22 02:11

Chris Conover