• 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: Asymmetrical behavior in NSMutableArray
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Asymmetrical behavior in NSMutableArray


  • Subject: Re: Asymmetrical behavior in NSMutableArray
  • From: Shawn Erickson <email@hidden>
  • Date: Wed, 9 Mar 2005 21:40:23 -0800


On Mar 9, 2005, at 9:06 PM, Serge Meynard wrote:

Greetings,

I just discovered something interesting behavior-wise in NSMutableArray... Since the class does not handle duplicates, you can add the same object several times to an array and it will appear in several slots. But if you then call removeObject: on that same object, ALL INSTANCES of the object are removed from the array! I wrote a short test app to confirm that I wasn't dreaming this up.

Is this normal, expected behavior? Shouldn't the class stick to never handling duplicates no matter the method call?

The API docs for NSMutableArray say [1]...

"Removes all occurrences of anObject in the receiver. This method uses indexOfObject: to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined on the basis of an object’s response to the isEqual: message."

So yes this is expected and documented behavior of that method.

Maybe what you want to use is... removeObjectIdenticalTo: ? (not knowing what behavior you want)

"Removes all occurrences of anObject in the receiver. This method uses the indexOfObjectIdenticalTo: method to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined using object addresses."

Or add your own method to do exactly what you want (easy to do with a category).

-Shawn

[1] <http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ ObjC_classic/Classes/NSMutableArray.html#//apple_ref/doc/uid/20000138/ BABDAJBB>

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Asymmetrical behavior in NSMutableArray
      • From: Serge Meynard <email@hidden>
References: 
 >Asymmetrical behavior in NSMutableArray (From: Serge Meynard <email@hidden>)

  • Prev by Date: Re: NSWindow setTitle not affecting displayed title
  • Next by Date: Re: Asymmetrical behavior in NSMutableArray
  • Previous by thread: Re: [newbie] SIGSEGV and GDB
  • Next by thread: Re: Asymmetrical behavior in NSMutableArray
  • Index(es):
    • Date
    • Thread