• 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: Using NSImages as keys to a dictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using NSImages as keys to a dictionary


  • Subject: Re: Using NSImages as keys to a dictionary
  • From: "Adam R. Maxwell" <email@hidden>
  • Date: Sat, 13 May 2006 09:19:32 -0700


On May 13, 2006, at 01:29, Greg Hurrell wrote:

El 13/05/2006, a las 8:24, Lawrence Sanbourne escribió:

My app needs a way of caching scaled images (thumbnails) so that they
aren't recreated each time. I first did this using an
NSMutableDictionary mapping from original image to scaled image.
However, this didn't work because apparently one original image wasn't
isEqual: to another.


I realized that I just wanted pointer equality. So I did this:

// originalIcon is an NSImage *
[cachedIcons objectForKey:[NSNumber numberWithInt:(int) originalIcon]];


It works beautifully. However, this code seems really weird to me. Is
there a better way?

According to the docs objects added to dictionaries are retained but keys are copied. So when you use your thumbnail as a key it is actually getting copied, thus changing the pointer. As you've discovered "isEqual:" evidently isn't doing a low-level comparison to see if it's really the same image and the equality test was failing.


Although the test you describe looks "weird" to you it is in fact the way you would do it if what you actually want to compare on is pointer equality. The down-side of this approach is that you then have to worry about the lifespan of the thumbnail objects seeing as the dictionary is neither copying nor retaining them.

Another way to do this might be to create a CFMutableDictionary with custom callbacks that retain keys instead of copying them, and compares keys based on pointer equality; with toll-free bridging, you'd just treat it as a standard NSMutableDictionary. You have to ensure that the hash of the keys (NSImage instances) doesn't change while they're in the dictionary, though.


--
Adam


_______________________________________________ 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: Using NSImages as keys to a dictionary
      • From: "Michael Ash" <email@hidden>
References: 
 >Using NSImages as keys to a dictionary (From: "Lawrence Sanbourne" <email@hidden>)
 >Re: Using NSImages as keys to a dictionary (From: Greg Hurrell <email@hidden>)

  • Prev by Date: Rotating Image Button
  • Next by Date: [Moderator] EOT Re: Should objects returned by indirection be autoreleased
  • Previous by thread: Re: Using NSImages as keys to a dictionary
  • Next by thread: Re: Using NSImages as keys to a dictionary
  • Index(es):
    • Date
    • Thread