Skip to content

Commit a33d37f

Browse files
committed
Use NSHashTable instead of NSOrderedSet to store checkBoxes weakly
1 parent d778782 commit a33d37f

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

Classes/BEMCheckBox.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ @interface BEMCheckBox ()
3535

3636
/** The group this box is associated with.
3737
*/
38-
@property (weak, nonatomic, nullable) BEMCheckBoxGroup *group;
38+
@property (strong, nonatomic, nullable) BEMCheckBoxGroup *group;
3939

4040
@end
4141

Classes/BEMCheckBoxGroup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/** An array of check boxes in this group.
1616
*/
17-
@property (nonatomic, strong, nonnull, readonly) NSOrderedSet<BEMCheckBox *> *checkBoxes;
17+
@property (nonatomic, strong, nonnull, readonly) NSHashTable *checkBoxes;
1818

1919
/** 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.
2020
*/

Classes/BEMCheckBoxGroup.m

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
@interface BEMCheckBoxGroup ()
1313

14-
@property (nonatomic, strong, nonnull) NSOrderedSet<BEMCheckBox *> *checkBoxes;
14+
@property (nonatomic, strong, nonnull) NSHashTable *checkBoxes;
1515

1616
@end
1717

1818
/** Defines private methods that we can call on the check box.
1919
*/
2020
@interface BEMCheckBox ()
2121

22-
@property (weak, nonatomic, nullable) BEMCheckBoxGroup *group;
22+
@property (strong, nonatomic, nullable) BEMCheckBoxGroup *group;
2323

2424
- (void)_setOn:(BOOL)on animated:(BOOL)animated notifyGroup:(BOOL)notifyGroup;
2525

@@ -31,7 +31,7 @@ - (instancetype)init {
3131
self = [super init];
3232
if (self) {
3333
_mustHaveSelection = NO;
34-
_checkBoxes = [NSOrderedSet orderedSet];
34+
_checkBoxes = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
3535
}
3636
return self;
3737
}
@@ -52,9 +52,8 @@ - (void)addCheckBoxToGroup:(nonnull BEMCheckBox *)checkBox {
5252

5353
[checkBox _setOn:NO animated:NO notifyGroup:NO];
5454
checkBox.group = self;
55-
NSMutableOrderedSet *mutableBoxes = [self.checkBoxes mutableCopy];
56-
[mutableBoxes addObject:checkBox];
57-
self.checkBoxes = [NSOrderedSet orderedSetWithOrderedSet:mutableBoxes];
55+
56+
[self.checkBoxes addObject:checkBox];
5857
}
5958

6059
- (void)removeCheckBoxFromGroup:(nonnull BEMCheckBox *)checkBox {
@@ -64,9 +63,7 @@ - (void)removeCheckBoxFromGroup:(nonnull BEMCheckBox *)checkBox {
6463
}
6564

6665
checkBox.group = nil;
67-
NSMutableOrderedSet *mutableBoxes = [self.checkBoxes mutableCopy];
68-
[mutableBoxes removeObject:checkBox];
69-
self.checkBoxes = [NSOrderedSet orderedSetWithOrderedSet:mutableBoxes];
66+
[self.checkBoxes removeObject:checkBox];
7067
}
7168

7269
#pragma mark Getters
@@ -97,7 +94,7 @@ - (void)setSelectedCheckBox:(BEMCheckBox *)selectedCheckBox {
9794
// Selection is nil
9895
if(self.mustHaveSelection && [self.checkBoxes count] > 0){
9996
// We must have a selected checkbox, so re-call this method with the first checkbox
100-
self.selectedCheckBox = [self.checkBoxes firstObject];
97+
self.selectedCheckBox = [self.checkBoxes anyObject];
10198
} else {
10299
for (BEMCheckBox *checkBox in self.checkBoxes) {
103100
BOOL shouldBeOn = NO;
@@ -114,7 +111,7 @@ - (void)setMustHaveSelection:(BOOL)mustHaveSelection {
114111

115112
// If it must have a selection and we currently don't, select the first box
116113
if (mustHaveSelection && !self.selectedCheckBox) {
117-
self.selectedCheckBox = [self.checkBoxes firstObject];
114+
self.selectedCheckBox = [self.checkBoxes anyObject];
118115
}
119116
}
120117

Sample Project/CheckBoxTests/GroupTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ - (void)testAutoSelectFirstCheckBox {
6767
XCTAssertNil(group.selectedCheckBox);
6868

6969
group.mustHaveSelection = YES;
70-
XCTAssertEqual(group.selectedCheckBox, [self.checkBoxes firstObject]);
70+
XCTAssertNotNil(group.selectedCheckBox);
7171

7272
group.selectedCheckBox = nil;
73-
XCTAssertEqual(group.selectedCheckBox, [self.checkBoxes firstObject]);
73+
XCTAssertNotNil(group.selectedCheckBox);
7474
}
7575

7676
- (void)testAddCheckBox {

0 commit comments

Comments
 (0)