Amazon.com Widgets removeObject & isEqual
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 23, 2013, 07:11:42 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 15 - Numbers, Strings, and Collections
| | | |-+  removeObject & isEqual
Pages: [1]   Go Down
Print
Author Topic: removeObject & isEqual  (Read 1008 times)
tbo
Newbie
*
Posts: 2






« on: June 16, 2009, 03:49:11 PM »

Hi all,

My understanding is that, with NSMutableArray, removeObject: will run a isEqual: on all items in the array and remove it if it returns true.

I tried to see what happens if I always return true in isEqual. It works fine, all items are removed from the array.
But ...
When I tried to return always false in isEqual, I was expecting that no item would be removed. And that is not the case!

For example:
In AddressCard.m
-(BOOL) isEqual: (AddressCard *) theCard
{
   return NO;
}

In AddressBook.m
-(void) removeCard: (AddressCard *) theCard
{
   [book removeObject: theCard];
}

In Test.m:
... (five cards: card1, card2, card3, card4 and card5 in the book)

   NSLog(@"Entries before remove: %i", [myBook entries]);
   [myBook removeCard: card5];
   NSLog(@"Entries after remove: %i", [myBook entries]);
...

As isEqual always returns NO, why do I have the following result: Entries before remove: 5,  Entries after remove: 4 and not "Entries after remove: 5".

Thanks for your help.
Logged
esc
Global Moderator
Full Member
*****
Posts: 230






« Reply #1 on: June 16, 2009, 04:54:11 PM »

Hi,

A few things to check:

1. "If two objects are equal, they must have the same hash value. This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass." -- see documentation for isEqual:

2. Did you define isEqual: elsewhere?

3. Double check the implementation of entries -- it just returns the count?
Logged
tbo
Newbie
*
Posts: 2






« Reply #2 on: June 21, 2009, 09:44:59 AM »

Hi, thanks for your answer.

isEqual is defined in AddressCard.h

-(BOOL) isEqual: (AddressCard *) theCard;

And implemented in AddressCard.m

-(BOOL) isEqual: (AddressCard *) theCard
{
   return NO;
}

entries is defined in AddressBook.h
-(int) entries;

and implemented in AddressBook.m
-(int) entries
{
   return [book count];
}

And "book" corresponds to:

@interface AddressBook : NSObject {
   NSString       *bookName;
   NSMutableArray *book;
}

Also AddressBook.h
-(void) removeCard: (AddressCard *) theCard;

And AddressBook.m
-(void) removeCard: (AddressCard *) theCard
{
   [book removeObject: theCard];
}

So I still do not understand why is one element removed when isEqual always return NO. It is called, because when it return always TRUE instead of NO, all elements are removed.
Logged
esc
Global Moderator
Full Member
*****
Posts: 230






« Reply #3 on: June 21, 2009, 11:23:30 AM »

So I still do not understand why is one element removed when isEqual always return NO. It is called, because when it return always TRUE instead of NO, all elements are removed.

Try checking the hash value -- you may need to define hash in your AddressCard class too.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3104







« Reply #4 on: June 21, 2009, 01:31:16 PM »

Okay, so I did a test and discovered that the isEqual: method does not get called if the two objects have identical values (that is, the pointers are the same and therefore reference the same object).  This is an obvious optimization, and that means you can't trick it!

Cheers,

Steve Kochan    
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.