NSMapTable with C strings as keys
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