<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Cocoa foreach Macro</title>
	<atom:link href="http://mjtsai.com/blog/2006/07/15/cocoa-foreach-macro/feed/" rel="self" type="application/rss+xml" />
	<link>http://mjtsai.com/blog/2006/07/15/cocoa-foreach-macro/</link>
	<description></description>
	<lastBuildDate>Sat, 20 Mar 2010 14:01:58 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Michael</title>
		<link>http://mjtsai.com/blog/2006/07/15/cocoa-foreach-macro/comment-page-1/#comment-55247</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Sun, 18 Mar 2007 02:22:28 +0000</pubDate>
		<guid isPermaLink="false">http://mjtsai.com/blog/?p=1286#comment-55247</guid>
		<description>Here&#039;s an &lt;a href=&quot;http://www.ghosttiger.com/?p=134&quot; rel=&quot;nofollow&quot;&gt;interesting alternative&lt;/a&gt;. Personally, I think it&#039;s too verbose, though.</description>
		<content:encoded><![CDATA[<p>Here's an <a href="http://www.ghosttiger.com/?p=134" rel="nofollow">interesting alternative</a>. Personally, I think it's too verbose, though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://mjtsai.com/blog/2006/07/15/cocoa-foreach-macro/comment-page-1/#comment-13376</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Sun, 16 Jul 2006 21:09:59 +0000</pubDate>
		<guid isPermaLink="false">http://mjtsai.com/blog/?p=1286#comment-13376</guid>
		<description>Yes, I used &quot;## object&quot; in my original macro for the same reason. I just don&#039;t see any reason to prefer the &quot;## object&quot; to using loop-scope variables. In any case, both work, so I&#039;ll stop discussing this.

It think the casts are perfectly safe, but I agree with Matt that they&#039;re a bit unsightly. Though so are the for-loops in their own way, but that&#039;s why we have a macro to hide it all. :-)</description>
		<content:encoded><![CDATA[<p>Yes, I used "## object" in my original macro for the same reason. I just don't see any reason to prefer the "## object" to using loop-scope variables. In any case, both work, so I'll stop discussing this.</p>
<p>It think the casts are perfectly safe, but I agree with Matt that they're a bit unsightly. Though so are the for-loops in their own way, but that's why we have a macro to hide it all. :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wincent Colaiuta</title>
		<link>http://mjtsai.com/blog/2006/07/15/cocoa-foreach-macro/comment-page-1/#comment-13375</link>
		<dc:creator>Wincent Colaiuta</dc:creator>
		<pubDate>Sun, 16 Jul 2006 18:31:06 +0000</pubDate>
		<guid isPermaLink="false">http://mjtsai.com/blog/?p=1286#comment-13375</guid>
		<description>Ugh, that nested for loops solution looks pretty ghastly. What&#039;s wrong with the original casts? In all cases the types involved are pointers (id, IMP etc) so there is no danger in casting them.</description>
		<content:encoded><![CDATA[<p>Ugh, that nested for loops solution looks pretty ghastly. What's wrong with the original casts? In all cases the types involved are pointers (id, IMP etc) so there is no danger in casting them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wincent Colaiuta</title>
		<link>http://mjtsai.com/blog/2006/07/15/cocoa-foreach-macro/comment-page-1/#comment-13374</link>
		<dc:creator>Wincent Colaiuta</dc:creator>
		<pubDate>Sun, 16 Jul 2006 18:28:18 +0000</pubDate>
		<guid isPermaLink="false">http://mjtsai.com/blog/?p=1286#comment-13374</guid>
		<description>If you were really worried about collisions of two &quot;_i&quot; variables (really &quot;_object&quot;) then you could switch to using &quot;## collection&quot; instead; I can&#039;t think of any reason why you&#039;d have nested enumerators iterating over the same collection. The reason why I chose &quot;object&quot; to introduce some uniqueness into the names is that in my usage patterns when using nested enumerators I find that I&#039;m always using two different types of object and so the variable names will naturally be different; things like &quot;title&quot; and &quot;author&quot;, or &quot;inner&quot; and &quot;outer&quot; and so forth.</description>
		<content:encoded><![CDATA[<p>If you were really worried about collisions of two "_i" variables (really "_object") then you could switch to using "## collection" instead; I can't think of any reason why you'd have nested enumerators iterating over the same collection. The reason why I chose "object" to introduce some uniqueness into the names is that in my usage patterns when using nested enumerators I find that I'm always using two different types of object and so the variable names will naturally be different; things like "title" and "author", or "inner" and "outer" and so forth.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://mjtsai.com/blog/2006/07/15/cocoa-foreach-macro/comment-page-1/#comment-13344</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Sat, 15 Jul 2006 19:36:23 +0000</pubDate>
		<guid isPermaLink="false">http://mjtsai.com/blog/?p=1286#comment-13344</guid>
		<description>Well, the truth is that I use the debugger only rarely. I prefer logging.

Anyway, I think your version with ## will have the same problem in the debugger. You&#039;ll also have two i&#039;s, as well as two of each of the helper _i variables. The main difference is that if you use i for one loop and j for the other, your helper variables will have unique names in the debugger and mine won&#039;t. That doesn&#039;t bother me because I&#039;ll probably be ignoring the helper variables, anyway, and because it&#039;s probably bad style to use nested loops both with i.

The main reason I like the loop-scoping (and the ## in the old version of my macro) is that it prevents collisions of the helper variable names when two foreach loops are in sequence or nested.</description>
		<content:encoded><![CDATA[<p>Well, the truth is that I use the debugger only rarely. I prefer logging.</p>
<p>Anyway, I think your version with ## will have the same problem in the debugger. You'll also have two i's, as well as two of each of the helper _i variables. The main difference is that if you use i for one loop and j for the other, your helper variables will have unique names in the debugger and mine won't. That doesn't bother me because I'll probably be ignoring the helper variables, anyway, and because it's probably bad style to use nested loops both with i.</p>
<p>The main reason I like the loop-scoping (and the ## in the old version of my macro) is that it prevents collisions of the helper variable names when two foreach loops are in sequence or nested.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wincent Colaiuta</title>
		<link>http://mjtsai.com/blog/2006/07/15/cocoa-foreach-macro/comment-page-1/#comment-13342</link>
		<dc:creator>Wincent Colaiuta</dc:creator>
		<pubDate>Sat, 15 Jul 2006 19:17:19 +0000</pubDate>
		<guid isPermaLink="false">http://mjtsai.com/blog/?p=1286#comment-13342</guid>
		<description>Yes, it will compile and run, but I think it&#039;s bad practice to rely on the behaviour in general. What do you do if you&#039;re in the debugger looking at nested enumerations?

    for (int i = 0; i &lt; 5; i++)
    {
        for (int i = 0; i &lt; 5; i++)
        {
            // what do I do here if I want to print the value of the outer &quot;i&quot;?
        }
    }</description>
		<content:encoded><![CDATA[<p>Yes, it will compile and run, but I think it's bad practice to rely on the behaviour in general. What do you do if you're in the debugger looking at nested enumerations?</p>
<p>    for (int i = 0; i &lt; 5; i++)<br />
    {<br />
        for (int i = 0; i &lt; 5; i++)<br />
        {<br />
            // what do I do here if I want to print the value of the outer "i"?<br />
        }<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://mjtsai.com/blog/2006/07/15/cocoa-foreach-macro/comment-page-1/#comment-13341</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Sat, 15 Jul 2006 17:38:33 +0000</pubDate>
		<guid isPermaLink="false">http://mjtsai.com/blog/?p=1286#comment-13341</guid>
		<description>I haven&#039;t seen any problems with namespace clashes. Nesting a foreach creates a new scope for its variables.</description>
		<content:encoded><![CDATA[<p>I haven't seen any problems with namespace clashes. Nesting a foreach creates a new scope for its variables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wincent Colaiuta</title>
		<link>http://mjtsai.com/blog/2006/07/15/cocoa-foreach-macro/comment-page-1/#comment-13339</link>
		<dc:creator>Wincent Colaiuta</dc:creator>
		<pubDate>Sat, 15 Jul 2006 17:16:53 +0000</pubDate>
		<guid isPermaLink="false">http://mjtsai.com/blog/?p=1286#comment-13339</guid>
		<description>When I saw your original article I wrote some macros of my own, documented here:

http://wincent.com/a/knowledge-base/archives/2006/01/objectivec_enum.php

It&#039;s a good improvement that you&#039;ve used loop-local variables. I did that too. But if you want to be able to nest enumerations you&#039;ll find that you still get namespace clashes. One way to solve this is with the concatenation operator (##). To illustrate (don&#039;t know if this will be readable or the code will get eaten but going to paste it in anyway):

#define WO_ENUMERATE(collection, object)                                                                                        \
for (id WOMacroEnumerator_ ## object    = [collection objectEnumerator],                                                        \
     WOMacroSelector_ ## object         = (id)@selector(nextObject),                                                            \
     WOMacroMethod_ ## object           = (id)[WOMacroEnumerator_ ## object methodForSelector:(SEL)WOMacroSelector_ ## object], \
     object                             = WOMacroEnumerator_ ## object ?                                                        \
     ((IMP)WOMacroMethod_ ## object)(WOMacroEnumerator_ ## object, (SEL)WOMacroSelector_ ## object) : nil;                   \
     object != nil;                                                                                                             \
     object = ((IMP)WOMacroMethod_ ## object)(WOMacroEnumerator_ ## object, (SEL)WOMacroSelector_ ## object))

/*! Perl-like syntax for WO_ENUMERATE. */
#define foreach(object, collection) WO_ENUMERATE(collection, object)</description>
		<content:encoded><![CDATA[<p>When I saw your original article I wrote some macros of my own, documented here:</p>
<p><a href="http://wincent.com/a/knowledge-base/archives/2006/01/objectivec_enum.php" rel="nofollow">http://wincent.com/a/knowledge-base/archives/2006/01/objectivec_enum.php</a></p>
<p>It's a good improvement that you've used loop-local variables. I did that too. But if you want to be able to nest enumerations you'll find that you still get namespace clashes. One way to solve this is with the concatenation operator (##). To illustrate (don't know if this will be readable or the code will get eaten but going to paste it in anyway):</p>
<p>#define WO_ENUMERATE(collection, object)                                                                                        \<br />
for (id WOMacroEnumerator_ ## object    = [collection objectEnumerator],                                                        \<br />
     WOMacroSelector_ ## object         = (id)@selector(nextObject),                                                            \<br />
     WOMacroMethod_ ## object           = (id)[WOMacroEnumerator_ ## object methodForSelector:(SEL)WOMacroSelector_ ## object], \<br />
     object                             = WOMacroEnumerator_ ## object ?                                                        \<br />
     ((IMP)WOMacroMethod_ ## object)(WOMacroEnumerator_ ## object, (SEL)WOMacroSelector_ ## object) : nil;                   \<br />
     object != nil;                                                                                                             \<br />
     object = ((IMP)WOMacroMethod_ ## object)(WOMacroEnumerator_ ## object, (SEL)WOMacroSelector_ ## object))</p>
<p>/*! Perl-like syntax for WO_ENUMERATE. */<br />
#define foreach(object, collection) WO_ENUMERATE(collection, object)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
