Gargoyle Gargoyle - 2 months ago 35
Swift Question

enumerateObjects failing on NSOrderedSet

In Swift 2 I was able to do this:

let foo = NSOrderedSet()
foo.enumerateObjects {
obj, idx, _ in
...
}


In Swift 3 I'm getting an error

Ambiguous use of 'enumerateObjects'


I'm not sure why the error is happening, or how to fix.

Answer

Try like this:

let foo = NSOrderedSet(array: [1, 2, 3, 1, 1, 6, 1])

for (offset,element) in foo.enumerated() {
    print(offset)
    print(element)
}

You can also do it using forEach as follow:

foo.enumerated().forEach {
    print($0.offset)
    print($0.element)
}

Using enumerateObjects the correct syntax would be:

foo.enumerateObjects({
    (obj, idx, _)  in
    print(obj)
    print(idx)
})