<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 12 Apr 2026 03:45:40 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Daniel - 2011-09-24 09:52:15</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#comments</link><description>Yet again, another great Q&amp;amp;A!
&lt;br /&gt;
&lt;br /&gt;I especially liked returning the &lt;code&gt;removalBlock&lt;/code&gt; as the token from &lt;code&gt;addObserverForName:…&lt;/code&gt; — very simple and elegant solution!</description><guid isPermaLink="true">ebc15fa5dd90a6fcdd8226ff4ec2ec42</guid><pubDate>Sat, 24 Sep 2011 09:52:15 GMT</pubDate></item><item><title>Steven - 2011-07-11 04:17:34</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#comments</link><description>As a topic for next time, how might one implement mocks/stubs in ObjC like RSpec has?</description><guid isPermaLink="true">86381c34fd953843dd174278678695e6</guid><pubDate>Mon, 11 Jul 2011 04:17:34 GMT</pubDate></item><item><title>mikeash - 2011-07-09 00:14:10</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#comments</link><description>You're right that that scenario would lead to endless recursion. I'll defer to an old article of mine for further info:
&lt;br /&gt;
&lt;br /&gt;&lt;a href="http://mikeash.com/pyblog/the-how-and-why-of-cocoa-initializers.html"&gt;http://mikeash.com/pyblog/the-how-and-why-of-cocoa-initializers.html&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;That probably says that you're right and I'm wrong here.
&lt;br /&gt;
&lt;br /&gt;Regarding &lt;code&gt;if(!a || !b)&lt;/code&gt;, it's not redundant. Technically it doesn't need to check &lt;code&gt;a&lt;/code&gt;, as the &lt;code&gt;isEqual:&lt;/code&gt; check will return NO if &lt;code&gt;a&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt;, although a separate check is slightly faster (and more explicit) for that case. The real importance is for &lt;code&gt;b&lt;/code&gt;, as the result of sending &lt;code&gt;nil&lt;/code&gt; to &lt;code&gt;isEqual:&lt;/code&gt; is not, as far as I know, well defined and could potentially throw an exception or crash.</description><guid isPermaLink="true">0006503ddbaf65e4c284abe1ff34d4e2</guid><pubDate>Sat, 09 Jul 2011 00:14:10 GMT</pubDate></item><item><title>Colin - 2011-07-08 21:52:14</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#comments</link><description>I know it's just an academic case, but if you'd subclass &lt;code&gt;_MANotificationCenterDictionaryKey&lt;/code&gt; overriding &lt;code&gt;init&lt;/code&gt; and calling &lt;code&gt;_initWithName:object:&lt;/code&gt;from there this could lead to endless recursion, couldn't it?
&lt;br /&gt;
&lt;br /&gt;Another remark: Isn't the &lt;code&gt;else if(!a || !b)&lt;/code&gt; branch of the &lt;code&gt;Equal&lt;/code&gt; function redundant?</description><guid isPermaLink="true">27e7affb52666fc028ea8cfe986959a6</guid><pubDate>Fri, 08 Jul 2011 21:52:14 GMT</pubDate></item><item><title>mikeash - 2011-07-08 21:01:33</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#comments</link><description>&lt;b&gt;Allan:&lt;/b&gt; Or use a zeroing weak reference! Yes, notifications and blocks get kind of irritating when not using garbage collection.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Colin:&lt;/b&gt; Just a habit not to use &lt;code&gt;super&lt;/code&gt; unless it's necessary. Since &lt;code&gt;init&lt;/code&gt; isn't overridden, it's not necessary here.</description><guid isPermaLink="true">3bdccc7bfed343133f25cd3a0505dfba</guid><pubDate>Fri, 08 Jul 2011 21:01:33 GMT</pubDate></item><item><title>Colin - 2011-07-08 20:41:42</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#comments</link><description>In the implementation of
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;- (id)_initWithName: (NSString *)name object: (id)obj&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;for
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;_MANotificationCenterDictionaryKey&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;is there a reason for writing
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;if((self = [self init]))&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;instead of
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;if((self = [super init]))&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;?</description><guid isPermaLink="true">876a5a9e95be7b68a7e99544b8de943a</guid><pubDate>Fri, 08 Jul 2011 20:41:42 GMT</pubDate></item><item><title>Allan - 2011-07-08 19:34:22</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#comments</link><description>I think it would be worth adding to the caveats section that block based notification handling is tricky from a memory management point of view.  If a block observing a notification references an object, then the object is retained by the block, and the block is retained by the notification center.  Many times, the logical place to unsubscribe from a notification is in the dealloc method of an object, but that won't work if a notification observing block retains the object that would unregister during deallocation.  When creating notification observing blocks, its often a good practice to make sure you're using a __block typed pointer to self instead of self itself.</description><guid isPermaLink="true">a010f77cdcafc064d000fdc164c77d65</guid><pubDate>Fri, 08 Jul 2011 19:34:22 GMT</pubDate></item></channel></rss>
