views:

921

answers:

2
// 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.
+1  A: 

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?

Meredith L. Patterson
Thanks for the help Meredith. The hashes are the same. Any other ideas on why the isEqual: comparison would fail?
James Skidmore
Hey Meredith, see my edit to the question. Maybe this will help.
James Skidmore
Nevermind, figured it out (see below). Thanks for your help though, I appreciate it!
James Skidmore
A: 

I had forgotten to initialize the array (*doh*):

[self setHBtns:[[NSMutableArray alloc] initWithCapacity:0]];
James Skidmore
That was actually going to be my next suggestion. Glad you got it sorted.
Meredith L. Patterson
You should also ensure that setHBtns: doesn't retain the array or you'll have a memory leak.
Jason Coco
Either that, or it **should** retain the array, and he could use `[NSMutableArray arrayWithCapacity:0]` (or just `[NSMutableArray array]`) instead. The bigger question in my mind is whether the property should be `readonly`, and the array should just be created once in the initializer of the containing class.
Quinn Taylor