• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Cannot get indexOfObject to work
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cannot get indexOfObject to work


  • Subject: Re: Cannot get indexOfObject to work
  • From: "Martin" <email@hidden>
  • Date: Fri, 19 Mar 2004 14:27:44 -0800

My guess is that NSArray calls isEqual on the object you pass in to indexOfObject, instead of each of the array elements themselves. Eg: you expect the internals to look something like:

isMatchAtIndex = [[array objectAtIndex:ii] isEqual:anObject];

When it is probably more like:

isMatchAtIndex = [anObject isEqual:[array objectAtIndex:ii]];

Of course

~Martin


On March 19 2004, Bartlomiej Cichosz <email@hidden> wrote:

>I have a custom class (let's call it CL) derived from NSObject, which
>implements isEqual: - currently, for debugging purposes it is defined
>as:
>
>- (BOOL)isEqual:(id)anObject {
> NSLog(@"isEqual called");
> return YES;
>}
>
>I also have an NSMutableArray containing number of CL objects. I am
>trying to use indexOfObject to locate particular object, but I get
>NSNotFound regardless of what I am searching for. My code looks as
>follows:
>
> if([[a objectAtIndex:6] isEqual:@"caption"]) {
> NSLog(@"Yes, they are the same");
> }
>
>
> i=[a indexOfObject:@"caption"];
> if(i==NSNotFound) {
> NSLog(@"Not found");
> }
>
>
>Variable a is the NSMutableArray I mentioned. The output I get is:
>
>2004-03-19 13:57:29.435 App[8533] isEqual called
>2004-03-19 13:57:29.435 App[8533] Yes, they are the same
>2004-03-19 13:57:29.435 App[8533] Not found
>
>It appears that isEqual is never called by indexOfObject, which seems
>to be contrary to documentation:
>
>
>indexOfObject:
>
>- (unsigned)indexOfObject:(id)anObject
> Searches the receiver for anObject and returns the lowest index whose
>corresponding array value is equal to anObject. Objects are considered
>equal if isEqual: returns YES. If none of the objects in the receiver
>is equal to anObject, indexOfObject: returns NSNotFound.
>
> See Also:  containsObject:,  indexOfObjectIdenticalTo:,  isEqual:
>(NSObject protocol)
>
>
>Is there a bug in implementation of indexOfObject, or am I
>understanding the documentation incorrectly? Of course, I can always
>implement search method myself, but it would be nice to know if I am
>doing something wrong :-)
>
>Thanks for any help,
>
>Bart
>_______________________________________________
>cocoa-dev mailing list | email@hidden
>Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Prev by Date: Re: memory management question
  • Next by Date: Re: Cannot get indexOfObject to work
  • Previous by thread: Re: Cannot get indexOfObject to work
  • Next by thread: Re: Cannot get indexOfObject to work
  • Index(es):
    • Date
    • Thread