1.swift的枚举只能拥有原始值和关联值的其中一种,不能同时拥有,会报错:Enum with raw type cannot have cases with arguments。
2.读取关联值时不能写参数的类型,否则编译报错。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| enum BinaryTree<Element> { case leaf indirect case node(v: Element, l: BinaryTree<Element>, r: BinaryTree<Element>) }
extension BinaryTree { init(_ val: Element) { self = .node(v: val, l: .leaf, r: .leaf) } }
extension BinaryTree { var values: [Element] { switch self { case .leaf: return [] case let .node(v, l, r): return l.values + [v] + r.values
} } }
|
参考
Enum with raw type cannot have cases with arguments