Convert Figma logo to code with AI

jflinter logoDwifft

Swift Diff

1,857
145
1,857
22

Top Related Projects

Simple way to identify what is different between 2 instances of any type. Must have for TDD.

🦀Amazingly incredible extraordinary lightning fast diffing in Swift

💻 A fast and flexible O(n) difference algorithm framework for Swift collection.

Quick Overview

Dwifft is a Swift library that provides efficient diffing algorithms for arrays and strings. It calculates the minimal edit distance between two sequences and can be used to determine the changes needed to transform one sequence into another. This library is particularly useful for updating UI elements, such as table views or collection views, with minimal changes.

Pros

  • Fast and efficient diffing algorithm for arrays and strings
  • Supports custom equality comparisons for complex objects
  • Integrates well with UITableView and UICollectionView for smooth animations
  • Written in Swift with a clean, modern API

Cons

  • Limited documentation and examples
  • Not actively maintained (last commit was in 2019)
  • May have performance issues with very large datasets
  • Lacks some advanced features found in more comprehensive diffing libraries

Code Examples

  1. Diffing two arrays of integers:
let oldArray = [1, 2, 3, 4, 5]
let newArray = [1, 3, 4, 5, 6]

let diff = Dwifft.diff(oldArray, newArray)
print(diff) // [.delete(1, 1), .insert(4, 6)]
  1. Using Dwifft with UITableView:
class MyTableViewController: UITableViewController, TableViewDiffCalculatorDelegate {
    var diffCalculator: TableViewDiffCalculator<String>?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        diffCalculator = TableViewDiffCalculator(tableView: tableView, initialRows: ["A", "B", "C"])
    }
    
    func updateRows(to newRows: [String]) {
        diffCalculator?.rows = newRows
    }
}
  1. Diffing two strings:
let oldString = "Hello, world!"
let newString = "Hello, Swift!"

let diff = Dwifft.diff(oldString, newString)
print(diff) // [.delete(7, "o"), .delete(8, "r"), .delete(9, "l"), .delete(10, "d"), .insert(7, "S"), .insert(8, "w"), .insert(9, "i"), .insert(10, "f"), .insert(11, "t")]

Getting Started

To use Dwifft in your project, follow these steps:

  1. Add Dwifft to your project using Swift Package Manager, CocoaPods, or Carthage.
  2. Import Dwifft in your Swift file:
import Dwifft
  1. Use the Dwifft.diff() function to calculate differences between arrays or strings:
let oldArray = [1, 2, 3]
let newArray = [2, 3, 4]
let diff = Dwifft.diff(oldArray, newArray)
  1. For UITableView or UICollectionView integration, use the TableViewDiffCalculator or CollectionViewDiffCalculator classes.

Competitor Comparisons

Simple way to identify what is different between 2 instances of any type. Must have for TDD.

Pros of Difference

  • More comprehensive diffing capabilities, including support for collections and custom types
  • Better performance, especially for large datasets
  • Actively maintained with regular updates and improvements

Cons of Difference

  • Steeper learning curve due to more complex API
  • Requires more setup and configuration for custom types
  • Larger codebase and potentially higher memory footprint

Code Comparison

Dwifft:

let diff = Dwifft.diff(oldArray, newArray)
tableView.animateRowChanges(oldData: oldArray, newData: newArray, withChanges: diff)

Difference:

let changes = diff(old: oldArray, new: newArray)
tableView.reload(changes: changes, updateData: {
    self.dataSource = newArray
})

Both libraries provide diffing functionality for Swift collections, but Difference offers more advanced features and better performance. Dwifft has a simpler API and may be easier to integrate for basic use cases. Difference is more suitable for complex scenarios and large datasets, while Dwifft might be preferable for smaller projects with simpler requirements. Consider your project's needs and complexity when choosing between the two libraries.

🦀Amazingly incredible extraordinary lightning fast diffing in Swift

Pros of DeepDiff

  • Supports more data types, including custom types
  • Offers more detailed diff results, including moves and patches
  • Provides better performance for large datasets

Cons of DeepDiff

  • More complex API, potentially steeper learning curve
  • Larger codebase and dependency footprint

Code Comparison

DeepDiff:

let changes = diff(old: oldArray, new: newArray)
for change in changes {
    switch change {
    case .insert(let index, let element):
        // Handle insertion
    case .delete(let index):
        // Handle deletion
    }
}

Dwifft:

let diff = Dwifft.diff(oldArray, newArray)
for diffStep in diff {
    switch diffStep {
    case .insert(let index, let element):
        // Handle insertion
    case .delete(let index, let element):
        // Handle deletion
    }
}

Both libraries provide similar functionality for diffing arrays, but DeepDiff offers more comprehensive diffing capabilities and supports additional data types. Dwifft has a simpler API and may be easier to integrate for basic diffing needs, while DeepDiff provides more advanced features and better performance for complex scenarios.

💻 A fast and flexible O(n) difference algorithm framework for Swift collection.

Pros of DifferenceKit

  • Better performance for large datasets due to optimized diffing algorithm
  • Supports sectioned data, allowing for more complex UI structures
  • More actively maintained with recent updates and improvements

Cons of DifferenceKit

  • Steeper learning curve due to more complex API
  • Larger codebase and dependency footprint
  • May be overkill for simple list updates

Code Comparison

DifferenceKit:

let changeset = StagedChangeset(source: oldItems, target: newItems)
tableView.reload(using: changeset) { data in
    dataSource = data
}

Dwifft:

let diff = oldItems.diff(newItems)
tableView.applyDiff(diff, withAnimation: .automatic)

DifferenceKit offers a more flexible API for complex scenarios, while Dwifft provides a simpler interface for basic diffing operations. DifferenceKit's performance advantages become more apparent with larger datasets, making it suitable for more demanding applications. However, Dwifft's straightforward approach may be preferable for simpler use cases or developers seeking a quick implementation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Build Status Current Version

Dwifft!

In 10 seconds

Dwifft is a small Swift library that tells you what the "diff" is between two collections, namely, the series of "edit operations" required to turn one into the other. It also comes with UIKit bindings, to automatically, animatedly keep a UITableView/UICollectionView in sync with a piece of data by making the necessary row/section insertion/deletion calls for you as the data changes.

Longer version

Dwifft is a Swift library that does two things. The first thing sounds interesting but perhaps only abstractly useful, and the other thing is a very concretely useful thing based off the first thing.

The first thing (found in Dwifft.swift) is an algorithm that calculates the diff between two collections using the Longest Common Subsequence method. If this kind of thing is interesting to you, there's a pretty great paper on diffing algorithms: http://www.xmailserver.org/diff2.pdf

The second thing (found in Dwifft+UIKit.swift) is a series of diff calculators for UITableViews and UICollectionViews. Let's say you have a UITableView that's backed by a simple array of values (like a list of names, e.g. ["Alice", "Bob", "Carol"]. If that array changes (maybe Bob leaves, and is replaced by Dave, so our list is now ["Alice, "Carol", "Dave"]), we'll want to update the table. The easiest way to do this is by calling reloadData on it. This has a couple of downsides: the transition isn't animated, and it'll cause your user to lose their scroll position if they've scrolled the table. The nicer way is to use the insertRowsAtIndexPaths:withRowAnimation and deleteRowsAtIndexPaths:withRowAnimation methods on UITableView, but this requires you to figure out which index paths have changed in your array (in our example, you'd have to figure out that the row at index 1 should be removed, and a new row should be inserted at index 2 should then be added). If only we had a way to diff the previous value of our array with it's new value. Wait a minute.

When you wire up a TableViewDiffCalculator to your UITableView (or a CollectionViewDiffCalculator to your UICollectionView, it'll automatically calculate diffs and trigger the necessary animations on it whenever you change its sectionedValues property. Neat, right? Notably, as of Dwifft 0.6, Dwifft will also figure out section insertions and deletions, as well as how to efficiently insert and delete rows across different sections, which is just so massively useful if you have a multi-section table. If you're currently using a <0.6 version of Dwifft and want to do this, read the 0.6 release notes.

Even longer version

Learn more about the history of Dwifft, and how it works, in this exciting video of a talk recorded at the Brooklyn Swift meetup in March 2017.

Why you should use Dwifft

  • Dwifft is useful - it can help you build a substantially better user experience if you have table/collection views with dynamic content in your app.
  • Dwifft is safe - there is some non-trivial index math inside of this diff algorithm that is easy to screw up. Dwifft has 100% test coverage on all of its core algorithms. Additionally, all of Dwifft's core functionality is tested with SwiftCheck, meaning it has been shown to behave correctly under an exhausting set of inputs and edge cases.
  • Dwifft is fast - a lot of time has been spent making Dwifft considerably (many orders of magnitude) faster than a naïve implementation. It almost certainly won't be the bottleneck in your UI code.
  • Dwifft is small - Dwifft believes (to the extent that a software library can "believe" in things) in the unix philosophy of small, easily-composed tools. It's unopinionated and flexible enough to fit into most apps, and leaves a lot of control in your hands as a developer. As such, you can probably cram it into your app in less than 5 minutes. Also, because it's small, it can actually achieve nice goals like 100% test and documentation coverage.

How to get started

  • First, you should take a look at the example app, to get a feel for how Dwifft is meant to be used.
  • Next, you should just sit down and read the entire documentation - it will take you <10 minutes, and you'll leave knowing everything there is to know about Dwifft.
  • Then, install Dwifft via cocoapods or carthage or whatever people are using these days.
  • Then get to Dwiffing.

Contributing

Contributions are welcome, with some caveats - please read the contributing guidelines before opening a PR to avoid wasting both our time.

Ok, that's it, there's nothing more here.