{"id":735,"date":"2003-12-08T10:02:25","date_gmt":"2003-12-08T15:02:25","guid":{"rendered":"\/?p=735"},"modified":"2014-07-14T10:11:41","modified_gmt":"2014-07-14T14:11:41","slug":"cocoa_enumeration","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2003\/12\/08\/cocoa_enumeration\/","title":{"rendered":"Cocoa Enumeration"},"content":{"rendered":"<p>\r\nJonathan Rentzsch is back with a post about the macros he uses to <a href=\"http:\/\/rentzsch.com\/papers\/improvingCocoaObjCEnumeration\">tame Cocoa&rsquo;s enumeration idiom<\/a>. Here&rsquo;s the macro I&rsquo;ve been using:\r\n<\/p> \r\n\r\n<pre>\r\n#define foreach(object, enumerator) \r\n    id mjtForeachEnumerator ## object = (enumerator); \r\n    if ( [mjtForeachEnumerator ## object respondsToSelector:@selector(objectEnumerator)] ) \r\n        mjtForeachEnumerator ## object = [mjtForeachEnumerator ## object objectEnumerator]; \r\n    SEL mjtNextObjectSEL ## object = @selector(nextObject); \r\n    IMP mjtNextObjectIMP ## object = [mjtForeachEnumerator ## object methodForSelector:mjtNextObjectSEL ## object]; \r\n    id object; \r\n    while ( (mjtForeachEnumerator ## object) &amp;&amp; \r\n            object = mjtNextObjectIMP ## object(mjtForeachEnumerator ## object, mjtNextObjectSEL ## object) )\r\n<\/pre>\r\n\r\n<ul class=\"loose\">\r\n<li>\r\nLike Rentzsch&rsquo;s macro, this will call <code>-objectEnumerator<\/code> for you. Rentzsch looks for objects that are <code>NSEnumerator<\/code>s and <em>doesn&rsquo;t<\/em> call <code>-objectEnumerator<\/code> in that case; I look for objects that support <code>-objectEnumerator<\/code> and <em>do<\/em> call it in that case.\r\n<\/li>\r\n<li>\r\nUnlike Rentzsch&rsquo;s macro, the control variables are not local to the loop. His way is better, but at the time I wrote this, it either wasn&rsquo;t possible to get the Objective-C compiler in C99 mode (without using Objective-C++) or I didn&rsquo;t know how to do it. Thus, I glue the element name to the end of each control variable name, to try to make it unique. This lets me use <code>foreach<\/code> more than once in a single scope.\r\n<\/li>\r\n<li>\r\nAfter using the macro for six months or so, I added the code to cache the <code>IMP<\/code> for <code>-nextObject<\/code>. All the code using <code>foreach<\/code> took advantage of the change with just a recompile. The speed increase was noticeable (without a stopwatch) in some cases. (In retrospect, I should probably have just used <code>@selector(nextObject)<\/code> instead of <code>mjtNextObjectSEL<\/code>, since <code>@selector<\/code> happens at link time, not runtime.)\r\n<\/li>\r\n<li>\r\nWe use the opposite parameter order. My macro is used like: <code>foreach( word, words )<\/code>, which means <code>foreach word in words<\/code>. (In fact, if you <em>really<\/em> wanted to, you could <code>#define<\/code> <code>in<\/code> to be a comma and then write <code>foreach ( word in words )<\/code>, but I don&rsquo;t recommend that.)\r\n<\/li>\r\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Jonathan Rentzsch is back with a post about the macros he uses to tame Cocoa&rsquo;s enumeration idiom. Here&rsquo;s the macro I&rsquo;ve been using: #define foreach(object, enumerator) id mjtForeachEnumerator ## object = (enumerator); if ( [mjtForeachEnumerator ## object respondsToSelector:@selector(objectEnumerator)] ) mjtForeachEnumerator ## object = [mjtForeachEnumerator ## object objectEnumerator]; SEL mjtNextObjectSEL ## object = @selector(nextObject); IMP mjtNextObjectIMP [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","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":[69,941,54,138,71],"class_list":["post-735","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-macros","tag-objective-c","tag-optimization","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/735","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=735"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/735\/revisions"}],"predecessor-version":[{"id":9137,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/735\/revisions\/9137"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}