1

My app checks if an NSString is empty when it launches, like this:

if ([checkfieldstring1 isEqualToString:@""]) {

    checkboxButton.hidden = YES;
}
else {

    checkboxbutton.hidden = NO;
}

However, when there is an empty string, the button is not being hidden. I know this method works when I hook it up as an IBAction to a button, but not on ViewDidLoad...

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 1
    Put a break point on the if... line and inspect checkfieldstring1. Could it be nil? – tassinari Apr 01 '12 at 01:23
  • Shift your code from `viewDidLoad` to `viewWillAppear:` and try again. I think this is a issue because some of the variables are `nil` until viewDidLoad is completely executed. Let me know if it helps you. – Parth Bhatt Apr 01 '12 at 17:56
  • @user1205856 Welcome to Stack Overflow (SO)! Please accept the answer that helps you the most in order to reward the people that help you. – lnafziger Apr 06 '12 at 17:15

4 Answers4

5

Checking for an empty string when the view is about to load needs to check for a nil string or an empty string as the string may not have been set up yet.

if (checkfieldstring1 == nil || [checkfieldstring1 length] == 0) {
    checkboxButton.hidden = YES;
} else {
    checkboxButton.hidden = NO;
}

Or, if you do as I do and have this in a set of common macros that I add to a project:

static inline BOOL isEmpty(id thing)) {
    return thing == nil
        || ([thing respondsToSelector:@selector(length)]
        && [(NSData *)thing length] == 0)
        || ([thing respondsToSelector:@selector(count)]
        && [(NSArray *)thing count] == 0);
}

Courtesy of Wil Shipley http://www.wilshipley.com/blog/2005/10/pimp-my-code-interlude-free-code.html

you could just check with:

if(isEmpty(checkfieldstring1) {
    checkboxButton.hidden = YES;
} else {
    checkboxButton.hidden = NO;
}
lnafziger
  • 25,760
  • 8
  • 60
  • 101
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • This still is not working... Now the button is hidden regardless if it is empty. – user1205856 Apr 01 '12 at 01:49
  • It's not working because the above code sets `checkfieldstring1` to `nil` before testing it. Change the "=" to "==". – Hot Licks Apr 01 '12 at 02:22
  • Also, you don't need to check for the string being nil -- if it is, its length method will return 0. –  Jul 17 '12 at 20:24
3

Since checkfieldstring1 can be nilor empty ("") you need to check for both cases.

The easiest way to check both, since Objective-C simply returns 0 when you send a message to a nil object is to use the length method, like this, instead:

if ([checkfieldstring1 length] == 0) {
    checkboxButton.hidden = YES;
}
else {
    checkboxbutton.hidden = NO;
}

You can also shorten this by just assigning the result of the comparison:

checkboxButton.hidden = [checkfieldstring1 length] == 0;

This might not be quite as clear when you are reading it later though, so I would tend towards the first example.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
1

I can't believe I was so stupid... For future reference, in my ViewDidLoad, I was setting the text of the textfield from UserDefaults. I was checking for an empty string BEFORE the text was set, so it appeared empty every time. Now, I placed the code after setting up the text field, and it works like a charm.

-1

i think this may helps you try this -

if(checkfieldstring1 == [NSNull null] || [checkfieldstring1 length] == 0) {
// do something
}
hchouhan02
  • 948
  • 1
  • 8
  • 18
  • an NSString instance will never be equal to [NSNull null] -- the most it can be is a simple nil or NULL. -1. –  Jul 17 '12 at 20:23