Add descendantNodeRepresentingObject to Node.swift.
This commit is contained in:
parent
5888e1b7d0
commit
7ba2fe72dd
@ -114,17 +114,13 @@ public final class Node: Hashable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func childNodeRepresentingObject(_ obj: AnyObject) -> Node? {
|
public func childNodeRepresentingObject(_ obj: AnyObject) -> Node? {
|
||||||
|
|
||||||
guard let childNodes = childNodes else {
|
return findNodeRepresentingObject(obj, recursively: false)
|
||||||
return nil
|
}
|
||||||
}
|
|
||||||
|
public func descendantNodeRepresentingObject(_ obj: AnyObject) -> Node? {
|
||||||
for oneNode in childNodes {
|
|
||||||
if oneNode.representedObject === obj {
|
return findNodeRepresentingObject(obj, recursively: true)
|
||||||
return oneNode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func hasAncestor(in nodes: [Node]) -> Bool {
|
public func hasAncestor(in nodes: [Node]) -> Bool {
|
||||||
@ -198,3 +194,23 @@ public extension Array where Element == Node {
|
|||||||
return self.map{ $0.representedObject }
|
return self.map{ $0.representedObject }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private extension Node {
|
||||||
|
|
||||||
|
func findNodeRepresentingObject(_ obj: AnyObject, recursively: Bool = false) -> Node? {
|
||||||
|
|
||||||
|
guard let childNodes = childNodes else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for childNode in childNodes {
|
||||||
|
if childNode.representedObject === obj {
|
||||||
|
return childNode
|
||||||
|
}
|
||||||
|
if recursively, let foundNode = childNode.descendantNodeRepresentingObject(obj) {
|
||||||
|
return foundNode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user