{"id":183,"date":"2008-12-16T19:29:59","date_gmt":"2008-12-17T00:29:59","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/12\/16\/isempty\/"},"modified":"2009-04-22T18:47:46","modified_gmt":"2009-04-22T23:47:46","slug":"isempty","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/12\/16\/isempty\/","title":{"rendered":"isEmpty?"},"content":{"rendered":"<p>Checking if a Cocoa object is empty is a little harder then in other languages, say C++, (but easier in some ways). Because every object in Objective-C is actually a <em>pointer<\/em> to an object, there are two ways, <code>obj<\/code>, can be empty.  <\/p>\n<p><strong><code>obj = {}<\/code><\/strong><\/p>\n<p>\n<code>obj<\/code> points to an object that is empty.  Say an array with 0 items, or the string <code>\"\"<\/code>, etc..<\/p>\n<p><strong><code>obj = nil<\/code><\/strong><\/p>\n<p>\n<code>obj<\/code>, the pointer <code>obj<\/code>, is <code>NULL<\/code>, <code>nil<\/code>, <code>0<\/code>, or whatever you want to call it.  You might argue that <code>obj<\/code> isn&#8217;t really an object, but it <em>is<\/em> empty, because there&#8217;s nothing in it.<\/p>\n<h3>Bug:<\/h3>\n<p>\nWhen I first started writing Objective-C, I made the mistake of writing code like: <code>if([name isEqualToString:@\"\"]){ ... }<\/code>, to test for empty strings.  And this code would work for a while until I used it in a situation where <code>name<\/code> was <code>nil<\/code>, and then, <a href=\"http:\/\/vgable.com\/blog\/2008\/05\/31\/messages-to-nowhere\/\">because sending any method called on <code>nil<\/code> &#8220;returns&#8221; <code>NO<\/code><\/a>, I would have mysterious errors.  (<a href=\"http:\/\/vgable.com\/blog\/2008\/12\/11\/there-are-worse-things-than-crashing\/\">Worse then a crash<\/a>, because it&#8217;s harder to track down.)<\/p>\n<p><H3>Bug:<\/H3> <\/p>\n<p>\nIt&#8217;s tempting to avoid the previous bug, by explicitly testing for <code>nil<\/code> <em>and<\/em> <code>{}<\/code>.  Say with code like:<\/p>\n<pre>if (email == nil || ![email isEqualTo:@\"\"] )\n&nbsp;&nbsp;&nbsp;email = @\"An email address is required\";<\/pre>\n<p>But generally this is a bad idea.  It means more code, which means more places for a bug.  I know it&#8217;s only one trivial test, but I&#8217;m serious, when I say it&#8217;s asking for a bug &#8212; like the bug in the example above, which sets <code> email <\/code> to <code>@\"An email address is required\"<\/code>, whenever it is <em>not<\/em> the empty string, rather then when it <em>is<\/em> empty.  (Values have been changed tho protect the innocent but it&#8217;s a bug I&#8217;ve seen.)<\/p>\n<p><H3>Solutions:<\/H3><\/p>\n<p><a href=\"http:\/\/www.wilshipley.com\/blog\/2005\/10\/pimp-my-code-interlude-free-code.html\">Wil Shipley suggests using the global function:<\/a><br \/>\n<code><br \/>\nstatic inline BOOL IsEmpty(id thing) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return thing == nil<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|| ([thing respondsToSelector:@selector(length)]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&& [(NSData *)thing length] == 0)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|| ([thing respondsToSelector:@selector(count)]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&& [(NSArray *)thing count] == 0);<br \/>\n}<br \/>\n<\/code><\/p>\n<p>I&#8217;ve been using his <code>IsEmpty()<\/code> for about a year. I&#8217;ve had zero problems with it, while it&#8217;s made my code more readable and concise.<\/p>\n<p>Another solution is to take advantage of <a href=\"http:\/\/vgable.com\/blog\/2008\/05\/31\/messages-to-nowhere\/\">what happens when you send a message to <code>nil<\/code><\/a>.  (To over-simplify, you get back 0 or <code>NO<\/code>.)  So you can just say &#8220;if <code>([obj count] == 0)<\/code> then <code>obj<\/code> is empty.&#8221;  This often means reversing your thinking, and testing &#8220;IsNotEmpty()&#8221; instead of &#8220;IsEmpty()&#8221;.  I don&#8217;t think it&#8217;s as clear is <code>IsEmpty()<\/code> in general, but in cases where it is, there you have it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Checking if a Cocoa object is empty is a little harder then in other languages, say C++, (but easier in some ways). Because every object in Objective-C is actually a pointer to an object, there are two ways, obj, can be empty. obj = {} obj points to an object that is empty. Say an [&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,5,4,13,8],"tags":[110,395,106,107,256,108,109,78,105],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-bug-bite","category-c","category-cocoa","category-objective-c","category-programming","category-sample-code","category-usability","tag-containers","tag-isequal","tag-nil","tag-nsarray","tag-nsdata","tag-nsdictionary","tag-nsset","tag-nsstring","tag-null"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/183","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=183"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}