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

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -