From a33d37fe35d286d12702c6c0d1522f46c910849b Mon Sep 17 00:00:00 2001 From: Boris Emorine Date: Sat, 13 May 2017 23:12:26 -0700 Subject: [PATCH] Use NSHashTable instead of NSOrderedSet to store checkBoxes weakly --- Classes/BEMCheckBox.m | 2 +- Classes/BEMCheckBoxGroup.h | 2 +- Classes/BEMCheckBoxGroup.m | 19 ++++++++----------- Sample Project/CheckBoxTests/GroupTests.m | 4 ++-- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Classes/BEMCheckBox.m b/Classes/BEMCheckBox.m index 6836d21..6dd9231 100755 --- a/Classes/BEMCheckBox.m +++ b/Classes/BEMCheckBox.m @@ -35,7 +35,7 @@ @interface BEMCheckBox () /** The group this box is associated with. */ -@property (weak, nonatomic, nullable) BEMCheckBoxGroup *group; +@property (strong, nonatomic, nullable) BEMCheckBoxGroup *group; @end diff --git a/Classes/BEMCheckBoxGroup.h b/Classes/BEMCheckBoxGroup.h index a399f94..3f67e61 100644 --- a/Classes/BEMCheckBoxGroup.h +++ b/Classes/BEMCheckBoxGroup.h @@ -14,7 +14,7 @@ /** An array of check boxes in this group. */ -@property (nonatomic, strong, nonnull, readonly) NSOrderedSet *checkBoxes; +@property (nonatomic, strong, nonnull, readonly) NSHashTable *checkBoxes; /** The currently selected check box. Only can be nil if mustHaveSelection is NO. Setting this value will cause the other check boxes to deselect automatically. */ diff --git a/Classes/BEMCheckBoxGroup.m b/Classes/BEMCheckBoxGroup.m index 978635c..8588784 100644 --- a/Classes/BEMCheckBoxGroup.m +++ b/Classes/BEMCheckBoxGroup.m @@ -11,7 +11,7 @@ @interface BEMCheckBoxGroup () -@property (nonatomic, strong, nonnull) NSOrderedSet *checkBoxes; +@property (nonatomic, strong, nonnull) NSHashTable *checkBoxes; @end @@ -19,7 +19,7 @@ @interface BEMCheckBoxGroup () */ @interface BEMCheckBox () -@property (weak, nonatomic, nullable) BEMCheckBoxGroup *group; +@property (strong, nonatomic, nullable) BEMCheckBoxGroup *group; - (void)_setOn:(BOOL)on animated:(BOOL)animated notifyGroup:(BOOL)notifyGroup; @@ -31,7 +31,7 @@ - (instancetype)init { self = [super init]; if (self) { _mustHaveSelection = NO; - _checkBoxes = [NSOrderedSet orderedSet]; + _checkBoxes = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; } return self; } @@ -52,9 +52,8 @@ - (void)addCheckBoxToGroup:(nonnull BEMCheckBox *)checkBox { [checkBox _setOn:NO animated:NO notifyGroup:NO]; checkBox.group = self; - NSMutableOrderedSet *mutableBoxes = [self.checkBoxes mutableCopy]; - [mutableBoxes addObject:checkBox]; - self.checkBoxes = [NSOrderedSet orderedSetWithOrderedSet:mutableBoxes]; + + [self.checkBoxes addObject:checkBox]; } - (void)removeCheckBoxFromGroup:(nonnull BEMCheckBox *)checkBox { @@ -64,9 +63,7 @@ - (void)removeCheckBoxFromGroup:(nonnull BEMCheckBox *)checkBox { } checkBox.group = nil; - NSMutableOrderedSet *mutableBoxes = [self.checkBoxes mutableCopy]; - [mutableBoxes removeObject:checkBox]; - self.checkBoxes = [NSOrderedSet orderedSetWithOrderedSet:mutableBoxes]; + [self.checkBoxes removeObject:checkBox]; } #pragma mark Getters @@ -97,7 +94,7 @@ - (void)setSelectedCheckBox:(BEMCheckBox *)selectedCheckBox { // Selection is nil if(self.mustHaveSelection && [self.checkBoxes count] > 0){ // We must have a selected checkbox, so re-call this method with the first checkbox - self.selectedCheckBox = [self.checkBoxes firstObject]; + self.selectedCheckBox = [self.checkBoxes anyObject]; } else { for (BEMCheckBox *checkBox in self.checkBoxes) { BOOL shouldBeOn = NO; @@ -114,7 +111,7 @@ - (void)setMustHaveSelection:(BOOL)mustHaveSelection { // If it must have a selection and we currently don't, select the first box if (mustHaveSelection && !self.selectedCheckBox) { - self.selectedCheckBox = [self.checkBoxes firstObject]; + self.selectedCheckBox = [self.checkBoxes anyObject]; } } diff --git a/Sample Project/CheckBoxTests/GroupTests.m b/Sample Project/CheckBoxTests/GroupTests.m index 06bf0f3..80c7d3b 100644 --- a/Sample Project/CheckBoxTests/GroupTests.m +++ b/Sample Project/CheckBoxTests/GroupTests.m @@ -67,10 +67,10 @@ - (void)testAutoSelectFirstCheckBox { XCTAssertNil(group.selectedCheckBox); group.mustHaveSelection = YES; - XCTAssertEqual(group.selectedCheckBox, [self.checkBoxes firstObject]); + XCTAssertNotNil(group.selectedCheckBox); group.selectedCheckBox = nil; - XCTAssertEqual(group.selectedCheckBox, [self.checkBoxes firstObject]); + XCTAssertNotNil(group.selectedCheckBox); } - (void)testAddCheckBox {