Make Node.childNodes non-optional.

This commit is contained in:
Brent Simmons 2018-02-25 22:01:09 -08:00
parent 7def2ae8d0
commit 6e6eefab27
2 changed files with 8 additions and 14 deletions

View File

@ -16,7 +16,7 @@ public final class Node: Hashable {
public let representedObject: AnyObject
public var canHaveChildNodes = false
public var isGroupItem = false
public var childNodes: [Node]?
public var childNodes = [Node]()
public let hashValue: Int
private static var incrementingID = 0
@ -28,7 +28,7 @@ public final class Node: Hashable {
}
public var numberOfChildNodes: Int {
return childNodes?.count ?? 0
return childNodes.count
}
public var indexPath: IndexPath {
@ -87,9 +87,6 @@ public final class Node: Hashable {
public func childAtIndex(_ index: Int) -> Node? {
guard let childNodes = childNodes else {
return nil
}
if index >= childNodes.count || index < 0 {
return nil
}
@ -98,7 +95,7 @@ public final class Node: Hashable {
public func indexOfChild(_ node: Node) -> Int? {
return childNodes?.index{ (oneChildNode) -> Bool in
return childNodes.index{ (oneChildNode) -> Bool in
oneChildNode === node
}
}
@ -189,9 +186,6 @@ 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

View File

@ -53,8 +53,8 @@ public final class TreeController {
return oneNode
}
if recurse, oneNode.canHaveChildNodes, let childNodes = oneNode.childNodes {
if let foundNode = nodeInArrayRepresentingObject(nodes: childNodes, representedObject: representedObject, recurse: recurse) {
if recurse, oneNode.canHaveChildNodes {
if let foundNode = nodeInArrayRepresentingObject(nodes: oneNode.childNodes, representedObject: representedObject, recurse: recurse) {
return foundNode
}
@ -89,7 +89,7 @@ private extension TreeController {
func visitNode(_ node: Node, _ visitBlock: NodeVisitBlock) {
visitBlock(node)
node.childNodes?.forEach{ (oneChildNode) in
node.childNodes.forEach{ (oneChildNode) in
visitNode(oneChildNode, visitBlock)
}
}
@ -117,14 +117,14 @@ private extension TreeController {
var childNodesDidChange = false
let childNodes = delegate?.treeController(treeController: self, childNodesFor: node)
let childNodes = delegate?.treeController(treeController: self, childNodesFor: node) ?? [Node]()
childNodesDidChange = !nodeArraysAreEqual(childNodes, node.childNodes)
if (childNodesDidChange) {
node.childNodes = childNodes
}
childNodes?.forEach{ (oneChildNode) in
childNodes.forEach{ (oneChildNode) in
if rebuildChildNodes(node: oneChildNode) {
childNodesDidChange = true
}