{"id":611,"date":"2010-06-02T03:08:54","date_gmt":"2010-06-02T08:08:54","guid":{"rendered":"http:\/\/vgable.com\/blog\/?p=611"},"modified":"2010-07-12T03:19:33","modified_gmt":"2010-07-12T08:19:33","slug":"nshomedirectory-is-a-bad-thing","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2010\/06\/02\/nshomedirectory-is-a-bad-thing\/","title":{"rendered":"NSHomeDirectory() is a Bad Thing"},"content":{"rendered":"<p>Code that uses <code>NSHomeDirectory()<\/code> is probably doing The Wrong Thing. It&#8217;s not appropriate to clutter up the user&#8217;s home directory &#8212; internal application-data should be stored in the <a href=\"http:\/\/cocoawithlove.com\/2010\/05\/finding-or-creating-application-support.html\"><code>Application Support<\/code> directory<\/a> (or a <a href=\"http:\/\/stackoverflow.com\/questions\/215820\/how-do-i-create-a-temporary-file-with-cocoa\">temporary file<\/a> if it&#8217;s transient). So I can&#8217;t think of a good reason to get the path to the user&#8217;s home directory. <strong>Every use of <code>NSHomeDirectory()<\/code> I&#8217;ve seen is spamming the home directory, or getting a subdirectory in a brittle way.<\/strong><\/p>\n<p>For sample code that gets a directory robustly, using <code> NSSearchPathForDirectoriesInDomains()<\/code>, see <a href=\"http:\/\/cocoawithlove.com\/2010\/05\/finding-or-creating-application-support.html\"><cite>Finding or creating the application support directory<\/cite><\/a>.<\/p>\n<p>Because <code>NSHomeDirectory()<\/code> encourages so many bad practices, it should be deprecated.<\/p>\n<h3>Disabling <code> NSHomeDirectory()<\/code> in Your Projects<\/h3>\n<p>Add the following macro to your prefix file:<\/p>\n<div class=\"codebox\" style=\"overflow:scroll\">\n<pre>#define NSHomeDirectory() NSHomeDirectory_IS_DISCOURAGED_USE_NSSearchPathForDirectoriesInDomains_TO_GET_A_SUBDIRECTORY_OF_HOME<\/pre>\n<\/div>\n<p>Then any use of <code>NSHomeDirectory()<\/code> will give the compiler error:<\/p>\n<div style=\"overflow:scroll\">\n<blockquote><p>error:<br \/>\n&#8216;NSHomeDirectory_IS_DISCOURAGED_USE_NSSearchPathForDirectoriesInDomains_TO_GET_A_SUBDIRECTORY_OF_HOME&#8217; undeclared (first use in this function)\n<\/p><\/blockquote>\n<\/div>\n<h3>Tell Me I&#8217;m Wrong<\/h3>\n<p><strong>If you&#8217;ve seen a legitimate use of <code>NSHomeDirectory()<\/code> please leave a comment!<\/strong> Just because I can&#8217;t think of one doesn&#8217;t mean they don&#8217;t exist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code that uses NSHomeDirectory() is probably doing The Wrong Thing. It&#8217;s not appropriate to clutter up the user&#8217;s home directory &#8212; internal application-data should be stored in the Application Support directory (or a temporary file if it&#8217;s transient). So I can&#8217;t think of a good reason to get the path to the user&#8217;s home directory. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,6,203,3,5,4],"tags":[241,582,159],"class_list":["post-611","post","type-post","status-publish","format-standard","hentry","category-announcement","category-cocoa","category-iphone","category-macosx","category-objective-c","category-programming","tag-best-practices","tag-nshomedirectory","tag-paths"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/611","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=611"}],"version-history":[{"count":5,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/611\/revisions"}],"predecessor-version":[{"id":649,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/611\/revisions\/649"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}