I have defined the following class and subclass:
import Foundation
class Node {
var key : String
private weak var parent: Node?
private var children: [Node] = []
init(key : String) {
self.key = key
}
func addChildNode(child: Node) {
children.append(child)
child.parent = self
}
func rootNode() -> Node {
var node = self
while node.parent != nil {
node = node.parent!
}
return node
}
}
class SubNode : Node {
var time : [Double] = []
}
When using the subclass and the method rootNode inherited, this returns the class Node and not the subclass SubNode. Therefore, I can not access the time property. One option is of course to cast explicitily the outcome of the rootNode as SubNode. But this is not very elegant. Any other idea?
var a = SubNode(key: "a")
a.time = [11,2,3]
var b = SubNode(key: "b")
var c = SubNode(key: "c")
a.addChildNode(b)
b.addChildNode(c)
c.rootNode() // c.rootNode can not access time unles casted as SubNode