• 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
NSMapTable with C strings as keys
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSMapTable with C strings as keys


  • Subject: NSMapTable with C strings as keys
  • From: Oleg Krupnov <email@hidden>
  • Date: Tue, 28 May 2013 08:46:17 +0300

I'd like to have a dictionary using C strings as keys (because I
already have const char* strings and would like to spare on creating
NSString wrappers) and C structures as values.

I thought using NSMapTable would be a good idea for this.

Here is my code:

// function for comparing string keys
static BOOL MapTableKeyComparator(NSMapTable* table, const void* key1,
const void* key2)
{
    int result = strcmp((const char*)key1, (const char*)key2);
    return result == 0;
}


NSMapTable* _table;

…

NSMapTableKeyCallBacks keyCallbacks = NSNonOwnedPointerMapKeyCallBacks;

keyCallbacks.isEqual = MapTableKeyComparator;

NSMapTableValueCallBacks valueCallbacks = NSNonOwnedPointerMapValueCallBacks;

_table = NSCreateMapTable(keyCallbacks, valueCallbacks, 0);

…

NSMapInsert(_table, name, value);

…

value = NSMapGet(_table, name);


Now, the problem is that sometimes when I try to get a value from the
table, the MapTableKeyComparator function is not called at all, and
NSMapGet returns NULL, thought immediate dump of the table shows that
all previous records are perfectly present in the table. The bug
happens sporadically, at different moments. Sometimes it happens at
the line of code where it perfectly worked on last debug session.

What could be wrong?

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: NSMapTable with C strings as keys
      • From: Eric Wing <email@hidden>
    • Re: NSMapTable with C strings as keys
      • From: Graham Cox <email@hidden>
    • Re: NSMapTable with C strings as keys
      • From: Jens Alfke <email@hidden>
  • Prev by Date: Re: Building an MVC based App for Mac OS X - NOT for iOS
  • Next by Date: Re: NSMapTable with C strings as keys
  • Previous by thread: Auto Layout & NSScrollViews
  • Next by thread: Re: NSMapTable with C strings as keys
  • Index(es):
    • Date
    • Thread