I presently have a singleton that holds the interpretation objects in my app. View controllers can subscribe to its delegate to allow them to replace their views if new translation objects have been added. That is what the category appears to be like like
class TranslationItems {
var delegate: TranslationItemsDelegate?
static let shared = TranslationItems()
personal var set = Set<Translation>()
// Public API
public func add(object: Translation) {
set.insert(object)
delegate?.newItemAdded()
}
public func take away(object: Translation) {
set.take away(object)
delegate?.someItemDeleted()
}
public func getAll() -> [Translation] {
return Array(set)
}
}
protocol TranslationItemsDelegate {
func newItemAdded()
func someItemDeleted()
}
View controllers can use it like this:
// Add or take away objects
TranslationItems.shared.add(object: translation)
// Subscribe for notifications
TranslationItems.shared.delegate = self
Does this singleton class look okay or may it’s improved?