I'm trying something like this in Swift but not working. Error is: Type () does not conform to type BooleanType
//visibleCollectionReusableHeaderViews is of type NSMapTable!
var enumerator: NSEnumerator = visibleCollectionReusableHeaderViews.objectEnumerator()
var myValue: AnyObject!
while (( myValue = enumerator.nextObject()))
{
}
What am I doing wrong? I don't think I understand how to iterate over an NSMapTable, or even just to get the first item in it.
In Swift, this is done using conditional assignment.
let enumerator = visibleCollectionReusableHeaderViews.objectEnumerator()
while let myValue: AnyObject = enumerator.nextObject() {
println(myValue)
}
Note the non optional type for myValue. Otherwise this loop would be infinite as myValue continued to accept nil objects.
Or a clearer and shorter approach (Swift 3):
for key in table.keyEnumerator() {
print(key)
}
for object in table.objectEnumerator() ?? NSEnumerator() {
print(object)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With