{"id":108,"date":"2008-06-28T14:58:00","date_gmt":"2008-06-28T22:58:00","guid":{"rendered":"http:\/\/eschatologist.net\/blog\/?p=108"},"modified":"2009-02-03T20:12:00","modified_gmt":"2009-02-04T04:12:00","slug":"always-use-notification-name-globals-not-string-literals","status":"publish","type":"post","link":"https:\/\/eschatologist.net\/blog\/?p=108","title":{"rendered":"Always use notification name globals, not string literals!"},"content":{"rendered":"<p>What&#8217;s wrong with this code?<\/p>\n<p>    &#8211; (void)registerForNotificationsFromTask:(NSTask *)task ( {<br \/>\n        [[NSNotificationCenter defaultCenter]<br \/>\n            addObserver:self<br \/>\n               selector:@selector(taskDidTerminateNotification:)<br \/>\n                   name:@&#8221;NSTaskDidTerminateNotification&#8221;<br \/>\n                 object:task];<br \/>\n    }<\/p>\n<p>If you didn&#8217;t notice anything wrong, look again.<\/p>\n<p>What&#8217;s bad about this is that it&#8217;s passing a **string literal** instead of a **global variable** for the notification name.  The code should really look like this:<\/p>\n<p>    &#8211; (void)registerForNotificationsFromTask:(NSTask *)task ( {<br \/>\n        [[NSNotificationCenter defaultCenter]<br \/>\n            addObserver:self<br \/>\n               selector:@selector(taskDidTerminateNotification:)<br \/>\n                   name:NSTaskDidTerminateNotification<br \/>\n                 object:task];<br \/>\n    }<\/p>\n<p>Isn&#8217;t that better?  (Among other things, Xcode will offer to complete the `NSTaskDidTerminateNotification` global variable for you \u00e2\u20ac\u201d unlike the contents of a string literal.)<\/p>\n<p>This is a bug that often results from copying &amp; pasting from documentation into code.  &#8220;I need this notification, it needs to be a string, so I&#8217;ll just put `@&#8221;&#8221;` around it.&#8221;  The *type* of a notification name is, in fact, `NSString` but you don&#8217;t have to pass a *string literal* for that.  Instead, pass the global variable that exists for each notification name and you&#8217;re guaranteed that the right thing will happen.<\/p>\n<p>If you&#8217;re creating and using your own notifications, be sure to follow the Cocoa pattern and create your own global variables containing the notification name.  Otherwise you&#8217;re at the mercy of typos within string literals.<\/p>\n<p>**Update:** Sanjay Samani helpfully pointed out that by *constant string* I meant *string literal*.  Thanks, Sanjay!  I&#8217;ve updated my post with this correction.  (Not sure where my memory was\u00e2\u20ac\u00a6)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What&#8217;s wrong with this code? &#8211; (void)registerForNotificationsFromTask:(NSTask *)task ( { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidTerminateNotification:) name:@&#8221;NSTaskDidTerminateNotification&#8221; object:task]; } If you didn&#8217;t notice anything wrong, look again. What&#8217;s bad about this is that it&#8217;s passing a **string literal** instead of a **global variable** for the notification name. The code should really look like this: &#8211; (void)registerForNotificationsFromTask:(NSTask *)task&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3],"tags":[9,18,17],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-technology","tag-cocoa","tag-objective-c","tag-xcode"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p74loH-1K","_links":{"self":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=108"}],"version-history":[{"count":3,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions\/111"}],"wp:attachment":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}