From 57f95193186ecfdbae37666fa22a9b5a1f38562a Mon Sep 17 00:00:00 2001 From: Erik Terwan Date: Tue, 19 Sep 2023 15:44:16 +0200 Subject: [PATCH] Update and remove should be non-optional to avoid escaping closures --- MMMArrayChanges.podspec | 4 ++-- .../MMMArrayChanges/Array+diffUpdate.swift | 16 +++++++-------- Sources/MMMArrayChanges/MMMArrayChanges.swift | 20 +++++++++---------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/MMMArrayChanges.podspec b/MMMArrayChanges.podspec index 301d328..df58b68 100644 --- a/MMMArrayChanges.podspec +++ b/MMMArrayChanges.podspec @@ -6,7 +6,7 @@ Pod::Spec.new do |s| s.name = "MMMArrayChanges" - s.version = "1.1.1" + s.version = "2.0.0" s.summary = "Helps finding (UITableView-compatible) differences between two arrays possibly of different types" s.description = s.summary s.homepage = "https://github.com/mediamonks/MMMArrayChanges" @@ -33,7 +33,7 @@ Pod::Spec.new do |s| end s.test_spec 'TestsSwift' do |test_spec| - test_spec.source_files = 'Tests/*.swift' + test_spec.source_files = 'Tests/**/*.swift' end s.default_subspec = 'ObjC', 'Swift' diff --git a/Sources/MMMArrayChanges/Array+diffUpdate.swift b/Sources/MMMArrayChanges/Array+diffUpdate.swift index 86cd41f..a676255 100644 --- a/Sources/MMMArrayChanges/Array+diffUpdate.swift +++ b/Sources/MMMArrayChanges/Array+diffUpdate.swift @@ -65,8 +65,8 @@ extension Array { elementId: (_ element: Element) -> ElementId, sourceArray: [SourceElement], sourceElementId: (_ sourceElement: SourceElement) -> ElementId, transform: (_ sourceElement: SourceElement) -> Element, - update: ((_ element: Element, _ sourceElement: SourceElement) -> Bool)? = nil, - remove: ((_ element: Element) -> Void)? = nil + update: ((_ element: Element, _ sourceElement: SourceElement) -> Bool), + remove: ((_ element: Element) -> Void) ) -> Bool { // We just use the compact logic here, since that will behave the same with a // non-optional transform closure. @@ -88,8 +88,8 @@ extension Array { elementId: (_ element: Element) -> ElementId, sourceArray: [SourceElement], sourceElementId: (_ sourceElement: SourceElement) -> ElementId, transform: (_ sourceElement: SourceElement) -> Element?, - update: ((_ element: Element, _ sourceElement: SourceElement) -> Bool)? = nil, - remove: ((_ element: Element) -> Void)? = nil + update: ((_ element: Element, _ sourceElement: SourceElement) -> Bool), + remove: ((_ element: Element) -> Void) ) -> Bool { var changed = false @@ -103,7 +103,7 @@ extension Array { // According to our index the current array already has a matching element, so just keep it... elementById.removeValue(forKey: id) // ...possibly updating. - if update?(element, sourceElement) ?? false { + if update(element, sourceElement) { // The update closure indicated that a change in the existing element should be counted // alongside with removals, additions and moves. changed = true @@ -142,10 +142,8 @@ extension Array { self = result // IDs left in the index correspond to elements missing in the new array, so they have to me marked as gone. - if let remove = remove { - elementById.forEach { (_, element) in - remove(element) - } + elementById.forEach { (_, element) in + remove(element) } } diff --git a/Sources/MMMArrayChanges/MMMArrayChanges.swift b/Sources/MMMArrayChanges/MMMArrayChanges.swift index 9dbf8a3..37b2eae 100644 --- a/Sources/MMMArrayChanges/MMMArrayChanges.swift +++ b/Sources/MMMArrayChanges/MMMArrayChanges.swift @@ -308,9 +308,9 @@ public class MMMArrayChanges: CustomStringConvertible, Equatable { - Parameters: - - update: Optional closure that's called for every element in the array that was not added to update its contents. + - update: Closure that's called for every element in the array that was not added to update its contents. - - remove: Optional closure that's called for every removed element of the array. + - remove: Closure that's called for every removed element of the array. Note that it should not try removing the corresponing element, it's only for your own book-keeping. - transform: A closure that should be able to creat a new element of the array from the corresponding element @@ -319,8 +319,8 @@ public class MMMArrayChanges: CustomStringConvertible, Equatable { public static func byUpdatingArray( _ array: inout [Element], elementId: (Element) -> ElementId, sourceArray: [SourceElement], sourceElementId: (SourceElement) -> ElementId, - update: ((_ element: Element, _ oldIndex: Int, _ sourceElement: SourceElement, _ newIndex: Int) -> Bool)? = nil, - remove: ((_ element: Element, _ oldIndex: Int) -> Void)? = nil, + update: ((_ element: Element, _ oldIndex: Int, _ sourceElement: SourceElement, _ newIndex: Int) -> Bool), + remove: ((_ element: Element, _ oldIndex: Int) -> Void), transform: (_ newElement: SourceElement, _ newIndex: Int) -> Element ) -> MMMArrayChanges { @@ -342,7 +342,7 @@ public class MMMArrayChanges: CustomStringConvertible, Equatable { // But let's check for item updates. var updates: [Update] = [] for i in 0..( _ array: inout [Element], sourceArray: [Element], - update: ((_ element: Element, _ oldIndex: Int, _ sourceElement: Element, _ newIndex: Int) -> Bool)? = nil, - remove: ((_ element: Element, _ oldIndex: Int) -> Void)? = nil + update: ((_ element: Element, _ oldIndex: Int, _ sourceElement: Element, _ newIndex: Int) -> Bool), + remove: ((_ element: Element, _ oldIndex: Int) -> Void) ) -> MMMArrayChanges { let result = byUpdatingArray( &array,