{"id":195,"date":"2008-12-28T19:58:32","date_gmt":"2008-12-29T00:58:32","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/12\/28\/fast-enumeration-in-objective-c-10\/"},"modified":"2008-12-28T19:58:33","modified_gmt":"2008-12-29T00:58:33","slug":"fast-enumeration-in-objective-c-10","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/12\/28\/fast-enumeration-in-objective-c-10\/","title":{"rendered":"Fast Enumeration In Objective-C 1.0"},"content":{"rendered":"<p>I have used <a href=\"http:\/\/vgable.com\/code\/ForEach.h\">ForEach.h<\/a> to do &#8220;<a href=\"http:\/\/vgable.com\/blog\/2008\/04\/07\/foreach-for-the-win\/\">Fast Enumeration<\/a>&#8221; in Mac OS X 10.4 &#8220;Tiger&#8221;, without any problems. You must set the \u201cC Language Dialect\u201d in Xcode to C99 for it to work.  Credit to <a href=\"http:\/\/mjtsai.com\/blog\/2006\/07\/15\/cocoa-foreach-macro\/\">Michael Tsai<\/a> for the code, you can find other implementations of it on <a href=\"http:\/\/mjtsai.com\/blog\/2006\/07\/15\/cocoa-foreach-macro\/\">his website<\/a>.<\/p>\n<p>With <a href=\"http:\/\/vgable.com\/code\/ForEach.h\">ForEach.h<\/a> you can do:<br \/>\n<code><br \/>\n#import \"ForEach.h\"<\/p>\n<p>foreach(thing, collection) {<br \/>\n&nbsp;&nbsp;&nbsp;\/*do stuff with thing*\/<br \/>\n}<\/p>\n<p>foreacht(NSString, name, allNames)<br \/>\n&nbsp;&nbsp;&nbsp;;\/\/name is typed as an NSString*<br \/>\n<\/code><\/p>\n<p>With a large collection, the <code>foreach<\/code> macro should be faster then using <code>NSEnumerator<\/code> or <code>objectAtIndex:<\/code>, because it calls the <code>nextObject<\/code> function directly, without going through Objective-C&#8217;s message sending.  I have not benchmarked it, because I haven&#8217;t had a reason to optimize the mechanics of my for-loops, so I don&#8217;t know by how much.  But it should give you the <a href=\"http:\/\/vgable.com\/blog\/2008\/09\/14\/fast-enumeration-really-is-faster\/\"><em>fast<\/em> in Fast Enumeration<\/a>.<\/p>\n<p>If you can&#8217;t use <a href=\"http:\/\/cocoawithlove.com\/2008\/05\/fast-enumeration-clarifications.html\">Objective-C 2.0&#8217;s Fast Enumeration<\/a> (requires Leopard or, iPhone), then I highly recommend <a href=\"http:\/\/vgable.com\/code\/ForEach.h\">ForeEach.h<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have used ForEach.h to do &#8220;Fast Enumeration&#8221; in Mac OS X 10.4 &#8220;Tiger&#8221;, without any problems. You must set the \u201cC Language Dialect\u201d in Xcode to C99 for it to work. Credit to Michael Tsai for the code, you can find other implementations of it on his website. With ForEach.h you can do: #import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,3,5,4,13],"tags":[275,274,134],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-macosx","category-objective-c","category-programming","category-sample-code","tag-objective-c-10","tag-tiger","tag-xcode"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}