swift树容器 - 找到父节点

时间:2016-05-27 22:43:16

标签: swift

我在swift 2.2中制作了如下的树容器。我想要一个容纳基类节点的容器树。我想要做的是找到一种制作anode.parentNode()函数的通用方法。

也许我不应该在swift中这样做,我对NodeElem中的var Container : AnyObject?也不满意。有什么建议? TIA

    //: Playground - noun: a place where people can play

    import Cocoa

    var str = "Hello, playground"


    class NodeElem {
        // points to the Heirarchy<T> container
        var container : AnyObject?
        func dump(indent: Int) {
            print("implement me")
        }
    }

    class Hierarchy<T : NodeElem> {
        var parent : Hierarchy<T>?
        var children : [Hierarchy<T>] = []
        var modified : Bool = false
        var value : T

        init(root : T) {
            self.value = root
            root.container = self
        }

        init(parent : Hierarchy<T>, node : T) {
            self.value = node
            self.parent = parent
            node.container = self
            parent.children.append(self)
        }

        func dump(indent : Int) {
            value.dump(indent)
            for c in children {
                //c.Value.Dump(indent)
                c.dump(indent+2)
            }
        }

        func parentNode() -> T {
            return (parent?.value)!
        }
    }

    class elem : NodeElem {
        var prop = "node"
        init(name: String) {
            prop = name
        }
        override func dump(indent: Int) {
            let leader = String(count: indent, repeatedValue: Character(" "))
            print(leader + prop)
        }
    }

    class anotherelem : NodeElem {
        var prop = "node"
        init(name: String) {
            prop = name
        }
        override func dump(indent: Int) {
            let leader = String(count: indent, repeatedValue: Character(" "))
            print("another " + leader + prop)
        }
    }

    extension NodeElem {

        }

    // this works but not sure if this is the best way
        func parentNode<T : NodeElem>(node : T) -> T? {
            let h = (node.container as! Hierarchy<T>)
            return h.parent?.value
        }




var root = Hierarchy<elem>(root: elem(name: "root"))
    var node = elem(name: "child1")
    var child1 = Hierarchy<elem>(parent: root, node: node)
    var child2 = Hierarchy<elem>(parent: root, node: elem(name: "child2"))
    var child11 = Hierarchy<elem>(parent: child1, node: elem(name: "child11"))
    var child12 = Hierarchy<elem>(parent: child1, node: elem(name: "child12"))
    var child21 = Hierarchy<elem>(parent: child2, node: elem(name: "child21"))
    var child22 = Hierarchy<elem>(parent: child2, node: elem(name: "child22"))
    root.dump(0)

    var aroot = Hierarchy<anotherelem>(root: anotherelem(name: "aroot"))
    var anode = anotherelem(name: "child1")
    var achild1 = Hierarchy<anotherelem>(parent: aroot, node: anode)
    var achild2 = Hierarchy<anotherelem>(parent: aroot, node: anotherelem(name: "child2"))
    var achild11 = Hierarchy<anotherelem>(parent: achild1, node: anotherelem(name: "child11"))
    var achild12 = Hierarchy<anotherelem>(parent: achild1, node: anotherelem(name: "child12"))
    var achild21 = Hierarchy<anotherelem>(parent: achild2, node: anotherelem(name: "child21"))
    var achild22 = Hierarchy<anotherelem>(parent: achild2, node: anotherelem(name: "child22"))
    aroot.dump(0)

    achild1.parentNode().dump(0)

    parentNode(anode)?.dump(0)

    // I'd like a 
    anode.parent().dump(0)

输出

root
  child1
    child11
    child12
  child2
    child21
    child22
another aroot
another   child1
another     child11
another     child12
another   child2
another     child21
another     child22
another aroot

1 个答案:

答案 0 :(得分:-1)

尝试:

class NodeElem<T : NodeElem> {
    var Container : Heirarchy<T>?
    func Dump(indent: Int) {
        print("implement me")
    }
}