NSMutableArray containsObject method not detecting object
-
22-07-2019 - |
Question
// Add the button to the NSMutableArray.
...
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[[self hBtns] addObject:btn];
...
// In another method, try to see if it exists.
- (void)didPushBtn:(id)sender
{
UIButton *btn = (UIButton *)sender;
if ([[self hBtns] containsObject:btn]) // Is false every time.
...
}
Why is it not detecting that the UIButton is in the array?
EDIT
It turns out that it won't even detect it right after it's added:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[[self hBtns] addObject:btn];
if ([[self hBtns] containsObject:btn]) // Returns false.
Solution 2
I had forgotten to initialize the array (*doh*):
[self setHBtns:[[NSMutableArray alloc] initWithCapacity:0]];
OTHER TIPS
Sounds like the isEqual:
comparison is failing. Can you take a look at the hash
for the UIButton in both places (where it's added, and then in didPushBtn
) and see if they're the same value?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow