I have this block of code in a view class I have:
private var notes: [String] = [] {
didSet {
if notes.count > 0, notes.count == (oldValue.count + 1) {
tableView.insertRows(at: [IndexPath.init(row: 0, section: 3)], with: .right)
} else {
tableView.reloadData()
}
}
}
private var images: [UIImage?] = [] {
didSet {
if images.count > 0, images.count == (oldValue.count + 1) {
tableView.insertRows(at: [IndexPath.init(row: 0, section: 2)], with: .right)
} else {
tableView.reloadData()
}
}
}
private var urls: [URL?] = [] {
didSet {
if urls.count > 0, urls.count == (oldValue.count + 1) {
tableView.insertRows(at: [IndexPath.init(row: 0, section: 1)], with: .right)
} else {
tableView.reloadData()
}
}
}
It works fantastically, but I see this and straight away I want to improve it because it's almost exactly the same thing three times.. I've used some Generics in Java, and I've read through the documentation for generics in Swift but I can't quite get it to work. I want to eventually do something like
private var items: [T] = [] {
didSet {
if items.count > 0, items.count == (oldValue.count + 1) {
tableView.insertRows(at: [IndexPath.init(row: 0, section: 1)], with: .right)
} else {
tableView.reloadData()
}
}
}
so that I could keep it all in one section. Is this possible? I'm still quite the newbie so I appreciate any help even if this is a simple question to you :)
Thanks!