Swift: Recursively cycle through all subviews to find a specific class and append to an array -


having devil of time trying figure out. asked similar question here: swift: subviews of specific type , add array

while works, realized there many subviews , sub-sub views, , need function starts @ main uiview, cycles through subviews (and subviews until there aren't left) , adds array custom button class have named checkcircle.

essentially i'd end array of checkcircles constitute checkcircles added view programmatically.

any ideas? here's i've been working on. doesn't seem appending checkcircles array:

    func getsubviewsofview(v:uiview) -> [checkcircle] {         var circlearray = [checkcircle]()         // subviews of view          var subviews = v.subviews          if subviews.count == 0 {             return circlearray         }          subview : anyobject in subviews{   if let viewtoappend = subview as? checkcircle {         circlearray.append(viewtoappend checkcircle)       }             getsubviewsofview(subview as! uiview)         }         return circlearray     } 

your main problem when call getsubviewsofview(subview as! uiview) (recursively, within function), aren't doing result.

you can delete count == 0 check, since in case for…in loop skipped. have bunch of unnecessary casts

assuming desire flat array of checkcircle instances, think adaptation of code should work:

func getsubviewsofview(v:uiview) -> [checkcircle] {     var circlearray = [checkcircle]()      subview in v.subviews as! [uiview] {         circlearray += getsubviewsofview(subview)          if subview checkcircle {             circlearray.append(subview as! checkcircle)         }     }      return circlearray } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -