Skip to content

Commit

Permalink
Use NSHashTable instead of NSOrderedSet to store checkBoxes weakly
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris-Em committed May 16, 2017
1 parent d778782 commit a33d37f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Classes/BEMCheckBox.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Classes/BEMCheckBoxGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/** An array of check boxes in this group.
*/
@property (nonatomic, strong, nonnull, readonly) NSOrderedSet<BEMCheckBox *> *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.
*/
Expand Down
19 changes: 8 additions & 11 deletions Classes/BEMCheckBoxGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

@interface BEMCheckBoxGroup ()

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

@end

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

@property (weak, nonatomic, nullable) BEMCheckBoxGroup *group;
@property (strong, nonatomic, nullable) BEMCheckBoxGroup *group;

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

Expand All @@ -31,7 +31,7 @@ - (instancetype)init {
self = [super init];
if (self) {
_mustHaveSelection = NO;
_checkBoxes = [NSOrderedSet orderedSet];
_checkBoxes = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
}
return self;
}
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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];
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sample Project/CheckBoxTests/GroupTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a33d37f

Please sign in to comment.