{"id":91,"date":"2008-07-17T22:07:50","date_gmt":"2008-07-18T03:07:50","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/07\/17\/null-terminated-argument-lists\/"},"modified":"2008-07-17T22:07:52","modified_gmt":"2008-07-18T03:07:52","slug":"null-terminated-argument-lists","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/07\/17\/null-terminated-argument-lists\/","title":{"rendered":"Null-Terminated Argument Lists"},"content":{"rendered":"<p>I was using <a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/Foundation\/Classes\/NSDictionary_Class\/Reference\/Reference.html#\/\/apple_ref\/occ\/clm\/NSDictionary\/dictionaryWithObjectsAndKeys:\">+[NSDictionary dictionaryWithObjectsAndKeys:]<\/a> to make a new dictionary, but one of the objects in the dictionary was the result of a call to a method that was returning <code>nil<\/code>, so the dictionary was incomplete.<\/p>\n<p>This got me thinking about <code>NULL<\/code>\/<code>nil<\/code> terminated argument lists.  I don&#8217;t think they are a great idea (the compiler should be able to handle the list-termination for you!), but I think they are an <em>especially bad idea<\/em> in Objective-C.<\/p>\n<p>The problem that <strong>it&#8217;s very common to have a <code>nil<\/code> object in Objective-C<\/strong>, relative to, say C++.  Many Cocoa methods return <code>nil<\/code> on error.  <a href=\"http:\/\/vincentgable.com\/blog\/2008\/05\/31\/messages-to-nowhere\/\">Since doing stuff with <code>nil<\/code> (generally) won&#8217;t cause an exception<\/a>, these <code>nil<\/code>s stick around much longer then in other languages.  As you can see, <code>nil<\/code> is a pretty poor choice of a sentinel value.<\/p>\n<p>It&#8217;s the 21st century! The compiler <em>could<\/em> tell an Obj-C method using a variable-argument-list how many arguments are in the list.  This is trivial when all arguments are of type <code>id<\/code>.  Since Obj-C methods use a radically different syntax from C functions, it shouldn&#8217;t effect existing C-code.  Unfortunately, I don&#8217;t see this being added, because Objective-C is already so mature.<\/p>\n<p>In the meantime.  <strong>Be a little more suspicious of any objective-C methods taking a <code>NULL<\/code>-terminated list<\/strong>.  I wish I had a perfect solution to avoid them, but I don&#8217;t!  Sometimes they are the best way to do something.  If you have a great work-around for constructing, say an <code>NSDictionary<\/code> with a variable number of key\/values please let me know!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was using +[NSDictionary dictionaryWithObjectsAndKeys:] to make a new dictionary, but one of the objects in the dictionary was the result of a call to a method that was returning nil, so the dictionary was incomplete. This got me thinking about NULL\/nil terminated argument lists. I don&#8217;t think they are a great idea (the compiler [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,7,6,12,5,4,8],"tags":[110,106,107,108,109,105],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-bug-bite","category-c","category-cocoa","category-design","category-objective-c","category-programming","category-usability","tag-containers","tag-nil","tag-nsarray","tag-nsdictionary","tag-nsset","tag-null"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/91","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=91"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}