我无法对此进行编码,基本上我需要做的是对于任何具有树结构的节点,我想返回它所采用的路径,请参阅代码
class Node {
let value: String
var parentNode: Node?
var childenNode = [Node]()
func appendNode(node: Node) {
childenNode.append(node)
node.parentNode = self.parentNode
}
func path(node: Node? = nil) -> (CeilingTypes, [ModuleTypes]) {
let node = node ?? self
let ceilTitle: CeilingTypes
let moduleTitle: [ModuleTypes]
// immplementation
return (ceilTitle, moduleTitle)
}
init(value: String) {
self.value = value
}
}
尝试进行for...in
循环,因为意识到我不能像对待数组那样对待它。为清楚起见,ceilingTypes
是树的第一级,而moduleTypes
是源自ceilingTypes
的所有子级
例如,如果树结构是这样的:
[Main]
/ \
[Ceiling_1] [Ceiling_2]
/ \ / \
[Module_A] [Module_B] [Module_C] [Module_D]
如果我运行path(Module_A)
,则返回应该给我[Main, [Ceiling_1]]
Ceilingtypes
与ModuleTypes
之间的关系,它们都是enum
s
enum CeilingTypes {
case KWIKLOC_REGAL
case KWIKLOC_PREMIUM
case KWIKLOC_CORPORATE
}
enum ModuleTypes {
case _1200X600_REGULAR_H_PATTERN
case _1200X1200
case _600X600
case _600X600_HEAVY_DUTY
case _1200X1200MM
}
这是树结构
struct MenuStructure {
static var structure: Node {
// All nodes
// First layour nodes
let mainMenu = Node(value: "Select Ceiling Type")
let KWIKLOC_REGAL = Node(value: "KWIKLOC Regal 24mm Alum. T-bar")
let KWIKLOC_PREMIUM = Node(value: "KWIKLOC Premium 15mm Alum. T-bar")
let KWIKLOC_CORPORATE = Node(value: "KWIKLOC Corporate 32mm Mitred Top Hat")
// Second layour nodes
let _1200X600_REGULAR_H_PATTERN = Node(value: "1200 X 600 Regular H Pattern")
let _1200X1200 = Node(value: "1200 X 1200")
let _600X600 = Node(value: "600 X 600")
let _600X600_HEAVY_DUTY = Node(value: "600 X 600 Heavy Duty (Main Tees at 600 Centers)")
let _1200X1200MM = Node(value: "1200 X 1200 mm")
// First layour - connections
mainMenu.appendNode(node: KWIKLOC_REGAL)
mainMenu.appendNode(node: KWIKLOC_PREMIUM)
mainMenu.appendNode(node: KWIKLOC_CORPORATE)
// Second layour - connections
KWIKLOC_REGAL.appendNode(node: _1200X600_REGULAR_H_PATTERN)
KWIKLOC_REGAL.appendNode(node: _1200X1200)
KWIKLOC_REGAL.appendNode(node: _600X600)
KWIKLOC_REGAL.appendNode(node: _600X600_HEAVY_DUTY)
KWIKLOC_PREMIUM.appendNode(node: _1200X600_REGULAR_H_PATTERN)
KWIKLOC_PREMIUM.appendNode(node: _1200X1200)
KWIKLOC_PREMIUM.appendNode(node: _600X600)
KWIKLOC_PREMIUM.appendNode(node: _600X600_HEAVY_DUTY)
KWIKLOC_CORPORATE.appendNode(node: _1200X1200MM)
return mainMenu
}
}
谢谢!