• 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: NSDictionary key's (copyWithZone?)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDictionary key's (copyWithZone?)


  • Subject: Re: NSDictionary key's (copyWithZone?)
  • From: Fritz Anderson <email@hidden>
  • Date: Wed, 25 Jun 2003 11:16:30 -0500

NSDictionary keys must implement the NSCopying protocol -- they must implement the method copyWithZone: . The NSDictionary needs to copy the keys to enforce immutability: The hashing of the dictionary would fail if some other owner of a key changed its value.

NSObject does not conform to NSCopying. Even if it did, conformance to protocols is not inheritable -- your subclass would have to implement copyWithZone:, either to copy its own instance variables or to verify that taking no action is the correct behavior.

As I recall, you have to make sure that any class you use as an NSDictionary key has to implement isEqual: and hash correctly. NSObject defaults these to pointer comparisons, but if you have a semantic definition for isEqual (that is, if two different objects can be equal), you have to define hash so that equal objects have the same hash.

IOBluetoothDevice, if I'm not mistaken, is an IOKit class, am I right? That would make it a C++ class. C++ classes and Objective-C classes are completely different things, and you cannot interchange pointers to their objects. In particular, you can't put a pointer to an IOBluetoothDevice into an NSDictionary either as a key or as a value.

-- F

On Wednesday, June 25, 2003, at 05:56 AM, daniel oberhoff wrote:

i have problems using my objects as keys to dictionaries. the dictionary seems to want to copy them (why? in c++ i always just use the pointer...). now i have objects that inherit from NSObject, i thought those implement the copying? well, i get an exception all the time, with my object and also with the IOBluetoothDevice (with that its not sooo surpsising as they are consts somehow....so i just use their mac address which is fine...).

--
Fritz Anderson - Consulting Programmer - Chicago, IL
Mail: <email@hidden>
Risumi: <http://resume.manoverboard.org>
_______________________________________________
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: WebFrame does not respond to loadRequest: ?
  • Next by Date: Re: Properly handling mouseEntered: events
  • Previous by thread: NSDictionary key's (copyWithZone?)
  • Next by thread: autorelease problem
  • Index(es):
    • Date
    • Thread