{"id":771,"date":"2012-07-29T07:42:51","date_gmt":"2012-07-29T11:42:51","guid":{"rendered":"http:\/\/bruji.com\/articles\/?p=771"},"modified":"2025-02-07T13:53:17","modified_gmt":"2025-02-07T17:53:17","slug":"isnotempty-a-short-objective-c-code-snippet","status":"publish","type":"post","link":"https:\/\/bruji.com\/blog\/2012\/07\/29\/isnotempty-a-short-objective-c-code-snippet\/","title":{"rendered":"isNotEmpty a Short Objective-C Code Snippet"},"content":{"rendered":"<p>Today <a href=\"http:\/\/inessential.com\/2012\/07\/28\/emptiness\">Brent Simmons improved the developer pool of reusable code<\/a> by posting his code for testing if the value of an object is empty. Most programmers start down this path with <em>NSString<\/em> where for most purposes an empty string is the same as nil. From there it grows to include <em>NSNull<\/em> which JSON parser will commonly return, as well as other common objects found in collections such as <em>NSNumber<\/em> and <em>NSDate<\/em> and the collections themselves <em>NSDictionary<\/em> and <em>NSArray<\/em>.<\/p>\n<p>If you take a look at his code you will notice it&#8217;s a function (same as <a href=\"http:\/\/www.wilshipley.com\/blog\/2005\/10\/pimp-my-code-interlude-free-code.html\">Wil Shipley&#8217;s<\/a>). Because I write almost exclusively Objective-C, a function call in my code makes me pause and think. While building an analytics module for <a href=\"https:\/\/ncredinburgh.com\/betting-sites-not-on-gamstop\/\">betting sites not on GamStop<\/a>, I realized that straightforward function calls often hamper the streamlined approach I prefer. I avoid this by using categories and get to call methods instead of functions. This also avoids the issue of having to write an optimized version of the function to remember to use on NSStrings. With a category, you can optimize each individual call for the specific object. So in the hope of improving that shared code pool, below is my implementation. Overwhelmingly (97.63% of the time so far) I want to do something with an object when it&#8217;s not empty, so my method is reversed. Also, isNotEmpty sent to a nil object will result in the correct NO value being returned.<\/p>\n<div class=\"code\">\n<pre>@interface NSString (NSStringExtensions)\n- (BOOL)isNotEmpty;\n@end\n\n@interface NSNull (NSNullExtensions)\n- (BOOL)isNotEmpty;\n@end\n\n@interface NSNumber (NSNumberExtensions)\n- (BOOL)isNotEmpty;\n@end\n\n@interface NSDate (NSDateExtensions)\n- (BOOL)isNotEmpty;\n@end\n\n@interface NSArray (NSArrayExtensions)\n- (BOOL)isNotEmpty;\n@end\n\n@interface NSDictionary (NSDictionaryExtensions)\n- (BOOL)isNotEmpty;\n@end\n\n\n@implementation NSString (NSStringExtensions)\n- (BOOL)isNotEmpty {\n\tif ([self length] &gt; 0)\n\t\treturn YES;\n\treturn NO;\n}\n@end\n\n@implementation NSNull (NSNullExtensions)\n- (BOOL)isNotEmpty {\n\treturn NO;\n}\n@end\n\n@implementation NSNumber (NSNumberExtensions)\n- (BOOL)isNotEmpty {\n\tif ([self integerValue] == 0)\n\t\treturn NO;\n\treturn YES;\n}\n@end\n\n@implementation NSDate (NSDateExtensions)\n- (BOOL)isNotEmpty {\n\tif (self != nil)\n\t\treturn YES;\n\treturn NO;\n}\n@end\n\n@implementation NSArray (NSArrayExtensions)\n- (BOOL)isNotEmpty {\n\tif ([self count])\n\t\treturn YES;\n\treturn NO;\n}\n@end\n\n@implementation NSDictionary (NSDictionaryExtensions)\n- (BOOL)isNotEmpty {\n\tif ([self count])\n\t\treturn YES;\n\treturn NO;\n}\n@end\n<\/pre>\n<\/div>\n<p>I wrote these categories in 2003 and haven&#8217;t thought about them since then. I have 674 total calls to these functions in my code (how I know the exact percentage on how often I use it). Since I recently discovered the easy to use <a href=\"http:\/\/weblog.bignerdranch.com\/334-isequal-vs-isequaltostring\/\">BNRTimeBlock<\/a> for testing performance I ran the three implementation 10 million times and came up with the following averages after 10 runs:<\/p>\n<div class=\"code\">\n<pre>Time for == King Kong\nConor's isNotEmpty:       0.165795\nBrent's RSStringIsEmpty:  0.182188\nWil's isEmpty:            0.860662\n\nTime for == (null)\nConor's isNotEmpty:       0.045048\nBrent's RSStringIsEmpty:  0.039325\nWil's isEmpty:            0.041882\n\nTime for == @\"\"\nConor's isNotEmpty:       0.167456\nBrent's RSStringIsEmpty:  0.186889\nWil's isEmpty:            0.329623\n\nAverage:                  0.224318\n<\/pre>\n<\/div>\n<p>This shows that at .000000022 seconds per call you should be using these easy to read and maintain functions and\/or methods all over your Objective-C code. Brent&#8217;s longer RSIsEmpty performs similar to Wil&#8217;s function as it includes the necessary call to <strong>respondsToSelector:<\/strong>. At these speeds I think the RSStringIsEmpty optimization is unnecessary except in the heaviest of loops.<\/p>\n<p>I do believe my version should be renamed <strong>isSet<\/strong> or <strong>isFull<\/strong> to avoid using &#8220;not&#8221; in the method name; but although one learns a lot in 10 years, I also have been using <strong>isNotEmpty<\/strong> for 10 years.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today Brent Simmons improved the developer pool of reusable code by posting his code for testing if the value of an object is empty. Most programmers start down this path with NSString where for most purposes an empty string is the same as nil. From there it grows to include NSNull which JSON parser will [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[96,115,97,95],"class_list":["post-771","post","type-post","status-publish","format-standard","hentry","category-cocoa","tag-categories","tag-cocoa","tag-code-snippet","tag-objective-c"],"_links":{"self":[{"href":"https:\/\/bruji.com\/blog\/wp-json\/wp\/v2\/posts\/771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bruji.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bruji.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bruji.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/bruji.com\/blog\/wp-json\/wp\/v2\/comments?post=771"}],"version-history":[{"count":16,"href":"https:\/\/bruji.com\/blog\/wp-json\/wp\/v2\/posts\/771\/revisions"}],"predecessor-version":[{"id":1336,"href":"https:\/\/bruji.com\/blog\/wp-json\/wp\/v2\/posts\/771\/revisions\/1336"}],"wp:attachment":[{"href":"https:\/\/bruji.com\/blog\/wp-json\/wp\/v2\/media?parent=771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bruji.com\/blog\/wp-json\/wp\/v2\/categories?post=771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bruji.com\/blog\/wp-json\/wp\/v2\/tags?post=771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}