mikeash.com pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#commentsmikeash.com Recent CommentsFri, 29 Mar 2024 10:03:14 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssDaniel - 2011-09-24 09:52:15http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#commentsYet again, another great Q&amp;A! <br /> <br />I especially liked returning the <code>removalBlock</code> as the token from <code>addObserverForName:…</code> — very simple and elegant solution!ebc15fa5dd90a6fcdd8226ff4ec2ec42Sat, 24 Sep 2011 09:52:15 GMTSteven - 2011-07-11 04:17:34http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#commentsAs a topic for next time, how might one implement mocks/stubs in ObjC like RSpec has?86381c34fd953843dd174278678695e6Mon, 11 Jul 2011 04:17:34 GMTmikeash - 2011-07-09 00:14:10http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#commentsYou're right that that scenario would lead to endless recursion. I'll defer to an old article of mine for further info: <br /> <br /><a href="http://mikeash.com/pyblog/the-how-and-why-of-cocoa-initializers.html">http://mikeash.com/pyblog/the-how-and-why-of-cocoa-initializers.html</a> <br /> <br />That probably says that you're right and I'm wrong here. <br /> <br />Regarding <code>if(!a || !b)</code>, it's not redundant. Technically it doesn't need to check <code>a</code>, as the <code>isEqual:</code> check will return NO if <code>a</code> is <code>nil</code>, although a separate check is slightly faster (and more explicit) for that case. The real importance is for <code>b</code>, as the result of sending <code>nil</code> to <code>isEqual:</code> is not, as far as I know, well defined and could potentially throw an exception or crash.0006503ddbaf65e4c284abe1ff34d4e2Sat, 09 Jul 2011 00:14:10 GMTColin - 2011-07-08 21:52:14http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#commentsI know it's just an academic case, but if you'd subclass <code>_MANotificationCenterDictionaryKey</code> overriding <code>init</code> and calling <code>_initWithName:object:</code>from there this could lead to endless recursion, couldn't it? <br /> <br />Another remark: Isn't the <code>else if(!a || !b)</code> branch of the <code>Equal</code> function redundant?27e7affb52666fc028ea8cfe986959a6Fri, 08 Jul 2011 21:52:14 GMTmikeash - 2011-07-08 21:01:33http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#comments<b>Allan:</b> Or use a zeroing weak reference! Yes, notifications and blocks get kind of irritating when not using garbage collection. <br /> <br /><b>Colin:</b> Just a habit not to use <code>super</code> unless it's necessary. Since <code>init</code> isn't overridden, it's not necessary here.3bdccc7bfed343133f25cd3a0505dfbaFri, 08 Jul 2011 21:01:33 GMTColin - 2011-07-08 20:41:42http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#commentsIn the implementation of <br /> <br /><code>- (id)_initWithName: (NSString *)name object: (id)obj</code> <br /> <br />for <br /> <br /><code>_MANotificationCenterDictionaryKey</code> <br /> <br />is there a reason for writing <br /> <br /><code>if((self = [self init]))</code> <br /> <br />instead of <br /> <br /><code>if((self = [super init]))</code> <br /> <br />?876a5a9e95be7b68a7e99544b8de943aFri, 08 Jul 2011 20:41:42 GMTAllan - 2011-07-08 19:34:22http://www.mikeash.com/?page=pyblog/friday-qa-2011-07-08-lets-build-nsnotificationcenter.html#commentsI 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.a010f77cdcafc064d000fdc164c77d65Fri, 08 Jul 2011 19:34:22 GMT