Tuesday, April 8, 2014

Exposing NSDictionary

Bartosz Ciechanowski:

While powerful, indexed ivars come with two caveats. First of all, class_createInstance can’t be used under ARC, so you’ll have to compile some parts of your class with -fno-objc-arc flag to make it shine. Secondly, the runtime doesn’t keep the indexed ivar size information anywhere. Even though dealloc will clean everything up (as it calls free internally), you should keep the storage size somewhere, assuming you use variable number of extra bytes.


We already know __NSDictionarySizes is some kind of array that stores different possible sizes of __NSDictionaryI.


It turns out __NSDictionaryI doesn’t check if the key passed into objectForKey: is nil (and I’d argue this is a good design decision). Calling hash method on nil returns 0, which causes the class to compare key at index 0 with nil. This is important: it is the stored key that executes the isEqual: method, not the passed in key.

See also Exposing NSMutableArray.

1 Comment RSS · Twitter

[…] Previously: Exploring Swift Dictionary’s Implementation, Exposing NSDictionary. […]

Leave a Comment