tonyarnold / Differ

Swift library to generate differences and patches between collections.
https://tonyarnold.github.io/Differ/
MIT License
662 stars 74 forks source link

Trouble with NSCollectionView usage #26

Closed rudedogg closed 6 years ago

rudedogg commented 6 years ago

I'm having some trouble using Differ with an NSCollectionView, specifically the function animateItemsAndSectionChanges(oldData: , newData:)

Here's a quick example of the code I'm using:

struct MySection: Equatable {
  let name = "Test"
  var items = [String]()
}
func quickRefreshTest(oldData: [MySection], newData: [MySection]) {
  collectionView.animateItemAndSectionChanges(oldData: oldData, newData: newData)
}

But the compiler complains Generic parameter 'T' could not be inferred.

Edit: This doesn't seem to be a bug, I think it was just a compiler/generics/protocols difficulty. I'll post more details tomorrow

tonyarnold commented 6 years ago

If there's something I can add (including documentation) - please let me know. I'd love to know more about what you saw.

rudedogg commented 6 years ago

Thanks @tonyarnold

I think the main issue I ran into was using Protocols as types in my datasource. That makes Equatable hard to implement, I had to use type erasure following https://khawerkhaliq.com/blog/swift-protocols-equatable-part-one/ as a guide. I've since switched to passing a nested Array of AnyEquatable{Protocol} to the calls of collectionView..animateItemAndSectionChanges(oldData: oldData, newData: newData), and the compiler is happy! It seems like using my struct above should work, but I think it's just a limitation of the Swift compiler.

I noticed a bug with the animations on NSCollectionView, so I'll open a PR for that.