Skip to content

Commit 34911f5

Browse files
committed
default implementations for Diffable protocol
1 parent cf54f5a commit 34911f5

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

DataSource/Diffable.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ public protocol Diffable {
1515
func isEqualToDiffable(_ other: Diffable?) -> Bool
1616
}
1717

18-
extension String: Diffable {
19-
20-
public var diffIdentifier: String {
21-
return self
18+
public extension Diffable where Self: Hashable {
19+
20+
var diffIdentifier: String {
21+
return String(hashValue)
2222
}
23+
}
24+
25+
public extension Diffable where Self: Equatable {
2326

24-
public func isEqualToDiffable(_ other: Diffable?) -> Bool {
25-
guard let other = other as? String else { return false }
27+
func isEqualToDiffable(_ other: Diffable?) -> Bool {
28+
guard let other = other as? Self else { return false }
2629
return self == other
2730
}
2831
}

Example/ViewControllers/Examples/DiffViewController.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,19 @@ class DiffViewController: UITableViewController {
7070

7171
// MARK: - Diff Item
7272

73-
struct DiffItem {
73+
struct DiffItem: Hashable, Equatable {
7474

7575
let value: Int
7676
let text: String
77-
let diffIdentifier: String
7877

7978
init(_ value: Int, text: String) {
8079
self.value = value
8180
self.text = text
82-
self.diffIdentifier = String(value)
8381
}
84-
}
85-
86-
extension DiffItem: Diffable {
8782

88-
public func isEqualToDiffable(_ other: Diffable?) -> Bool {
89-
guard let other = other as? DiffItem else { return false }
90-
return self.text == other.text
83+
var hashValue: Int {
84+
return value
9185
}
9286
}
87+
88+
extension DiffItem: Diffable { }

Example/ViewControllers/Examples/RandomPersonsViewController.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ extension Person: Diffable {
128128
public var diffIdentifier: String {
129129
return fullName
130130
}
131-
132-
func isEqualToDiffable(_ other: Diffable?) -> Bool {
133-
guard let other = other as? Person else { return false }
134-
return self == other
135-
}
136131
}
137132

138133
extension Person: CustomStringConvertible {

0 commit comments

Comments
 (0)