Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I hate to throw another log on this exceptionally old fire, but I'm leery about editing someone else's answer - especially when it's the selected answer.</p> <p>Jacob asked a follow up question: How can I do this with a single method call?</p> <p>The answer is, by creating a category - which basically extends the functionality of a base Objective-C class - and writing a "shorthand" method for all the other code. </p> <p>However, technically, a string with white space characters is not empty - it just doesn't contain any visible glyphs (for the last couple of years I've been using a method called isEmptyString: and converted today after reading this question, answer, and comment set).</p> <p>To create a category go to Option+Click -> New File... (or File -> New -> File... or just command+n) -> choose Objective-C Category. Pick a name for the category (this will help namespace it and reduce possible future conflicts) - choose NSString from the "Category on" drop down - save the file somewhere. (Note: The file will automatically be named NSString+YourCategoryName.h and .m.)</p> <p>I personally appreciate the self-documenting nature of Objective-C; therefore, I have created the following category method on NSString modifying my original isEmptyString: method and opting for a more aptly declared method (I trust the compiler to compress the code later - maybe a little too much).</p> <p>Header (.h):</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface NSString (YourCategoryName) /*! Strips the string of white space characters (inlcuding new line characters). @param string NSString object to be tested - if passed nil or @"" return will be negative @return BOOL if modified string length is greater than 0, returns YES; otherwise, returns NO */ + (BOOL)visibleGlyphsExistInString:(NSString *)string; @end </code></pre> <p>Implementation (.m):</p> <pre><code>@implementation NSString (YourCategoryName) + (BOOL)visibleGlyphsExistInString:(NSString *)string { // copying string should ensure retain count does not increase // it was a recommendation I saw somewhere (I think on stack), // made sense, but not sure if still necessary/recommended with ARC NSString *copy = [string copy]; // assume the string has visible glyphs BOOL visibleGlyphsExist = YES; if ( copy == nil || copy.length == 0 || [[copy stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0 ) { // if the string is nil, no visible characters would exist // if the string length is 0, no visible characters would exist // and, of course, if the length after stripping the white space // is 0, the string contains no visible glyphs visibleGlyphsExist = NO; } return visibleGlyphsExist; } @end </code></pre> <p>To call the method be sure to #import the NSString+MyCategoryName.h file into the .h or .m (I prefer the .m for categories) class where you are running this sort of validation and do the following:</p> <pre><code>NSString* myString = @""; // or nil, or tabs, or spaces, or something else BOOL hasGlyphs = [NSString visibleGlyphsExistInString:myString]; </code></pre> <p>Hopefully that covers all the bases. I remember when I first started developing for Objective-C the category thing was one of those "huh?" ordeals for me - but now I use them quite a bit to increase reusability.</p> <p>Edit: And I suppose, technically, if we're stripping characters, this:</p> <pre><code>[[copy stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0 </code></pre> <p>Is really all that is needed (it should do everything that category method does, including the copy), but I could be wrong on that score.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload