SelectiveEquatable

main

Check equality by selectively choosing which properties you'd like to compare.
DandyLyons/SelectiveEquatable

SelectiveEquatable

SelectiveEquatable is a Swift library that allows you to perform equality checks on object and value instances by specifying which properties to include in the comparison. Any combination of properties can be included in the comparison so long as they conform to the Equatable protocol.

Usage

Suppose we have the following Person struct:

struct Person: Identifiable {
   let firstName: String
   let lastName: String
   let age: Int
   let id: UUID
   let profileImage: UIImage
}

Suppose we had a collection of Persons and we'd like to determine if any persons have the same firstName, lastName, and age but different ids and profileImages. We can use SelectiveEquatable to perform this comparison:

extension Person: SelectiveEquatable {}
person1.isEqual(to: person2, by: \.firstName, \.lastName, \.age) // returns true or false

By simply conforming our type to SelectiveEquatable, we can now selectively compare instances of Person based on the properties we specify. In this case, we're comparing person1 and person2 based on their firstName, lastName, and age.

Motivation and Design

The Equatable protocol in Swift is great for comparing instances of a type based on all of their properties. However, there are times when we only want to compare instances based on a subset of their properties. There are also some scenarios where the cost of conforming to Equatable for a type is not practical. SelectiveEquatable was designed to address these issues. To learn more about the motivation and design of SelectiveEquatable, check out these blog posts:

Installation

SelectiveEquatable is an extremely simple protocol. In order to use it, you can simply copy the SelectiveEquatable.swift file into your project.

Swift Package Manager

You can also use the Swift Package Manager to install SelectiveEquatable. Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/DandyLyons/SelectiveEquatable", from: "1.0.0")
]

License

SelectiveEquatable is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue May 13 2025 00:08:03 GMT-0900 (Hawaii-Aleutian Daylight Time)