Recursive loop of an array of arrays using functional

I want to scroll through my subviews views, and for each subview loop through its subviews, etc.

so let's say I have the following code:

let view = myVC.view
view.backgroundColor = UIColor.clearColor()

then repeat the same for each view. I want to do this functionally.

Any insight is greatly appreciated.

EDIT:

to clarify

I am looking for something like this:

view.subviews.chanageColor() { (aView, aColor) in
aView.backgroundColor = aColor
}

but it must be recursive, it goes to each representation of the representation.

+4
source share
4 answers

Use a queue instead of recursion.

I think you could do something like this ...

extension UIView {    
    func changeColor(color: UIColor) {
        var queue = [self]

        while let view = queue.first() {
            queue += view.subviews

            view.backgroundColor = color

            queue.remove(view)
        }
    }
}

I would do it with a queue, not recursively. However, this method does not work.

Fulfillment of the desired question

, , .

extension UIView {
    // this name isn't very good but you get the idea
    func applyChangeToAllSubviews(block: (UIView) -> ()) {
        var queue = [self]

        // I don't have my dev computer dos not sure of the syntax
        while let view = queue.first() {
            queue += view.subviews

            block(view)

            queue.remove(view)
        }
    }
}

, ...

someView.applyChangeToAllSubviews {
    view in
    view.backgroundColor = UIColor.whiteColor()
}

, ... ish.

+2

- ?

func makeAllSubviewsClear(view: UIView!)
{
    view.backgroundColor = UIColor.clearColor()
    for eachSubview in view.subviews
    {
        makeAllSubviewsClear(eachSubview)
    }
}

:

let view = myVC.view
makeAllSubviewsClear(view)
+4

follwing:

let _ = myVC.view.subviews.map{ $0.backgroundColor = UIColor.clearColor() }

- imho.

EDIT: , OP , . $0.

- :

extension Array where Element: UIView {
    func changeColor(color: UIColor) {
        for view in self {
            view.subviews.changeColor(color)
            view.backgroundColor = color
        }
    }
}

:

myVC.view.subviews.changeColor(color)

@fogmaister :

extension Array where Element: UIView {
    func applyChange(closure: (UIView)->()) {
        for view in self {
            view.subviews.applyChange(closure)
            closure(view)
        }
    }
}
+1

, " ". UIView subviews, , .

(, ), a Generator, , forEach().

( Swift.)

extension SequenceType {

    public func generate<S : SequenceType where S.Generator.Element == Generator.Element>
        (children children: Generator.Element -> S) -> AnyGenerator<Generator.Element> {

            var selfGenerator = self.generate()
            var childGenerator : AnyGenerator<Generator.Element>?

            return anyGenerator {
                // Enumerate all children of the current element:
                if childGenerator != nil {
                    if let next = childGenerator!.next() {
                        return next
                    }
                }

                // Get next element of self, and prepare subGenerator to enumerate its children:
                if let next = selfGenerator.next() {
                    childGenerator = children(next).generate(children: children)
                    return next
                }

                return nil
            }
    }
}

, view UIView,

view.subviews.generate(children: { $0.subviews })

() () . forEach() , :

view.subviews.generate(children: { $0.subviews }).forEach {
    $0.backgroundColor = UIColor.clearColor()
}

:

let allViews = Array(view.subviews.generate(children: { $0.subviews }))
0

Source: https://habr.com/ru/post/1629912/


All Articles