Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

Swift - Improve computed properties in a class using Generics

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!

question from:https://stackoverflow.com/questions/65672094/swift-improve-computed-properties-in-a-class-using-generics

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could solve this with a simple function

private func updateTable(count: Int, oldCount: Int, section: Int) {
    if count > 0, count == (oldCount + 1) {
        tableView.insertRows(at: [IndexPath.init(row: 0, section: section)], with: .right)
    } else {
        tableView.reloadData()
    }
}

This would simplify the code to

private var notes: [String] = [] {
    didSet {
        updateTable(count: notes.count, oldCount: oldValue.count, section: 3)
    }
}

Another option is to pass the arrays

private func updateTable(array: [Element], oldArray: [Element], section: Int) {
    if array.count > 0, array.count == (oldArray.count + 1) {
        tableView.insertRows(at: [IndexPath.init(row: 0, section: section)], with: .right)
    } else {
        tableView.reloadData()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...