I created this singleton to access a shared array throughout my app:
class TranslationItems {
var delegate: TranslationItemsDelegate?
static let shared = TranslationItems()
var array = [Translation]() {
didSet {
delegate?.newItemAdded()
}
}
}
The problem is that this allows for duplication (the array may contain multiple items with the same hashValue). If I check for duplication inside the didSet
setter and then change the array there (for example by doing array = Array(Set(array))
) that leads to an infinite loop.
How do I remove duplicates in my class?