{"id":8677,"date":"2014-04-08T15:12:13","date_gmt":"2014-04-08T19:12:13","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=8677"},"modified":"2014-04-15T20:35:57","modified_gmt":"2014-04-16T00:35:57","slug":"exposing-nsdictionary","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/04\/08\/exposing-nsdictionary\/","title":{"rendered":"Exposing NSDictionary"},"content":{"rendered":"<p><a href=\"http:\/\/ciechanowski.me\/blog\/2014\/04\/08\/exposing-nsdictionary\/\">Bartosz Ciechanowski<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/ciechanowski.me\/blog\/2014\/04\/08\/exposing-nsdictionary\/\"><p>While powerful, indexed ivars come with two caveats. First of all, <code>class_createInstance<\/code> can&rsquo;t be used under ARC, so you&rsquo;ll have to compile some parts of your class with <code>-fno-objc-arc<\/code> flag to make it shine. Secondly, the runtime doesn&rsquo;t keep the indexed ivar size information anywhere. Even though <code>dealloc<\/code> will clean everything up (as it calls <code>free<\/code> internally), you should keep the storage size somewhere, assuming you use <em>variable<\/em> number of extra bytes.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>We already know <code>__NSDictionarySizes<\/code> is some kind of array that stores different possible sizes of <code>__NSDictionaryI<\/code>.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>It turns out <code>__NSDictionaryI<\/code> doesn&rsquo;t check if the <code>key<\/code> passed into <code>objectForKey:<\/code> is <code>nil<\/code> (and I&rsquo;d argue this is a <em>good<\/em> design decision). Calling <code>hash<\/code> method on <code>nil<\/code> returns <code>0<\/code>, which causes the class to compare key at index <code>0<\/code> with <code>nil<\/code>. This is important: it is the <em>stored<\/em> key that executes the <code>isEqual:<\/code> method, not the passed in key.<\/p><\/blockquote>\r\n\r\n<p>See also <a href=\"http:\/\/mjtsai.com\/blog\/2014\/03\/27\/exposing-nsmutablearray\/\">Exposing NSMutableArray<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Bartosz Ciechanowski: While powerful, indexed ivars come with two caveats. First of all, class_createInstance can&rsquo;t be used under ARC, so you&rsquo;ll have to compile some parts of your class with -fno-objc-arc flag to make it shine. Secondly, the runtime doesn&rsquo;t keep the indexed ivar size information anywhere. Even though dealloc will clean everything up (as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[770,55,69,860,54,138,71],"class_list":["post-8677","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-assembly-language","tag-arc","tag-cocoa","tag-nsdictionary","tag-objective-c","tag-optimization","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=8677"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8677\/revisions"}],"predecessor-version":[{"id":8711,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/8677\/revisions\/8711"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=8677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=8677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=8677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}