Iterating over an NSOrderedSet

后端 未结 3 1590
傲寒
傲寒 2021-02-18 18:40

I\'m trying to iterate over an instance of NSOrderedSet. Something like this:

func myFunc() {
    var orderedSet = NSOrderedSet(array: [ 42, 43, 44])

    for n          


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-18 19:29

    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)
    }
    

提交回复
热议问题