diff --git a/.gitignore b/.gitignore index 49efcace..d5330bcb 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ DerivedData *.xcuserstate *.xcscmblueprint .LSOverride +.swiftpm/ # CocoaPods # diff --git a/Source/Core/Section.swift b/Source/Core/Section.swift index 44561ddf..7aaa7d76 100644 --- a/Source/Core/Section.swift +++ b/Source/Core/Section.swift @@ -469,6 +469,19 @@ extension Section /* Helpers */ { newRow.wasAddedTo(section: self) } + /// Remove a row for tag represented as enum + /// - Parameter type: tag represented as enum + public func remove(tag: RowTagType) { + removeAll(where: { $0.tag == tag.rawValue as? String }) + } + + /// Remove all rows where tags are represented as `CaseIterable` enum cases + /// - Parameter type: type of the enum with tags + public func removeAllRowsWithIterableTags(type: RowTagType.Type) { + type.allCases.forEach { item in + remove(tag: item) + } + } } /** diff --git a/Tests/HelperMethodTests.swift b/Tests/HelperMethodTests.swift index 2359974f..901444e6 100644 --- a/Tests/HelperMethodTests.swift +++ b/Tests/HelperMethodTests.swift @@ -25,6 +25,12 @@ import XCTest @testable import Eureka +private enum RowTag: String, CaseIterable { + case first + case second + case third +} + class HelperMethodTests: BaseEurekaTests { func testRowByTag() { @@ -128,4 +134,33 @@ class HelperMethodTests: BaseEurekaTests { XCTAssertEqual(allSections.count, 6) } + func testRemoveForTag() { + let row = LabelRow(tag: RowTag.first.rawValue) + manySectionsForm[0] <<< row + XCTAssertEqual(manySectionsForm[0].rowBy(tag: RowTag.first.rawValue), row) + + manySectionsForm[0].remove(tag: RowTag.first) + let rowAfterRemove: LabelRow? = manySectionsForm[0].rowBy(tag: RowTag.first.rawValue) + XCTAssertNil(rowAfterRemove) + } + + func testRemoveAllRowsWithIterableTags() { + RowTag.allCases.forEach { tag in + manySectionsForm[0] <<< LabelRow(tag: tag.rawValue) + } + let someOtherRowTag = "someOtherRow" + let someOtherRow = LabelRow(tag: someOtherRowTag) + manySectionsForm[0] <<< someOtherRow + RowTag.allCases.forEach { tag in + let label: LabelRow? = manySectionsForm[0].rowBy(tag: tag.rawValue) + XCTAssertNotNil(label) + } + XCTAssertEqual(manySectionsForm[0].rowBy(tag: someOtherRowTag), someOtherRow) + manySectionsForm[0].removeAllRowsWithIterableTags(type: RowTag.self) + RowTag.allCases.forEach { tag in + let label: LabelRow? = manySectionsForm[0].rowBy(tag: tag.rawValue) + XCTAssertNil(label) + } + XCTAssertEqual(manySectionsForm[0].rowBy(tag: someOtherRowTag), someOtherRow) + } }