for loop - Special for syntax for looping over all objects of a certain class? -
can compress combined for
-plus-if
single for
,
i.e. can combine first 2 lines single instruction loop?
it should visit objects in childnodes
instances of mynode
.
for childnode in childnodes { if let mynode = childnode as? mynode { // mynode } }
i presume childnodes
array. if yes, can filter it:
for childnode in (childnodes.filter { $0 mynode }) { println ("it's node") }
or if prefer more explicit code:
let nodes = childnodes.filter { $0 mynode } childnode in nodes { println ("it's node") }
as far know, in swift there's no clean way combine for
loop optional binding skip iterations.
something might possible using standard for
loop, combined closure which, given index, returns next index containing mynode
instance... wouldn't call simplification in terms of code , readability:
let findnext = { (var index: int) -> (node: mynode?, next: int) in while index < childnodes.count { if let node = childnodes[index] as? mynode { return (node, index) } ++index } return (nil, index) } var (node, index) = findnext(0); node != nil; (node, index) = findnext(index + 1) { println ("it's node: \(node!)") }
Comments
Post a Comment