Базовая модель которая реализует общий протокол CompoundItemProtocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseCompoundItem: CompoundItemProtocol { | |
var children: [CompoundItemProtocol] = [] | |
let level: CompoundItemLevel | |
let identifier: String | |
var items: [CompoundItemProtocol] { | |
return children | |
} | |
// инициализация без параметров создает рутовый элемент структуры | |
init() { | |
self.identifier = "root" | |
self.level = .root | |
} | |
init(identifier: String, level: CompoundItemLevel) { | |
self.identifier = identifier | |
self.level = level | |
} | |
func add(_ model: CompoundItemProtocol...) { | |
self.children.append(contentsOf: model) | |
} | |
func remove(_ model: CompoundItemProtocol) { | |
if let index = self.children.index(where: { $0 == model }) { | |
children.remove(at: index) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment