0

At the moment I've been trying to use this if statement:

if (self.foodName.text == nil){
    NSLog(@"No Value")
}

Which doesn't seem to work. Is it something to do with return statements? If so, could someone please explain how to use return statements or point me in the right direction so I can understand them as I'm only a newbie to Objective-C.

Abbas
  • 14,186
  • 6
  • 41
  • 72
Mutch95
  • 271
  • 3
  • 14
  • This may help explain return statements: http://stackoverflow.com/questions/9339080/what-does-return-statement-mean-in-objective-c – stormwild Jan 23 '14 at 05:31

9 Answers9

1

Use this as NSString may contains white spaces i think best solution for validate NSString value.

NSCharacterSet *charSet = [NSCharacterSet whitespaceCharacterSet];
NSString *result = [self.foodName.text stringByTrimmingCharactersInSet:charSet];
if ([result isEqualToString:@""]) {
    NSLog(@"No Value Found");
}
Buntylm
  • 7,345
  • 1
  • 31
  • 51
  • Down voter warm welcome to you please provide the reason of down voting so that we can improve it ? – Buntylm Jan 23 '14 at 05:42
  • It kind of looks like someone just came in and marked all the answers down one or something, even though most look fine to me... I'll vote you up :) – ansible Jan 23 '14 at 05:53
1

What you have is correct, but you might not understand the different between an empty string and nil. Nil means the string has not been initalized yet and does not point to any memory address. This is different then a string that points to an empty string (@"").

Here is a small code snippet that you might (or might not) find useful

NSString *aString = nil;
NSLog(@"aString = %@",aString);
if (aString == nil)
{
    NSLog(@"aString is nil");
}

NSString *aSecondString = @"";
NSLog(@"aSecondString equals = %@",aSecondString);
if (aSecondString == nil)
{
    NSLog(@"aString is still nil!");
}
else if ([aSecondString isEqualToString:@""])
{
    NSLog(@"aString is not nil, but is an empty string");
}

The output looks like this

2014-01-22 23:29:16.314 TableViewCellWithAutoLayout[94886:a0b] aString = (null)
2014-01-22 23:29:16.315 TableViewCellWithAutoLayout[94886:a0b] aString is nil
2014-01-22 23:29:16.315 TableViewCellWithAutoLayout[94886:a0b] aSecondString equals = 
2014-01-22 23:29:16.315 TableViewCellWithAutoLayout[94886:a0b] aString is not nil, but is an empty string
ansible
  • 3,569
  • 2
  • 18
  • 29
0

An alternate way:

if (self.foodName.text.length == nil)
{
   NSLog(@"No Value")
}
spider1983
  • 1,098
  • 1
  • 7
  • 14
0

When your checking for nill, what your saying is if the object self.foodName.text is pointing at a empty memory space. What you want is:

if ([self.foodName.text isEqualToString:@""]) {
   NSLog(@"No Value")
}

This states if the value of the object self.foodName.text contains an empty string.

See How do I test if a string is empty in Objective C?

Community
  • 1
  • 1
Xuan
  • 460
  • 5
  • 13
0

Text fields and Text Views can have white spaces and/or new lines, so you have to trim them . Then you can check on the length

NSString *trimmedString = [self.foodName.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

// if self.foodName.text have value @" test \n"
// then trimmedString will have value @"test"

// if self.foodName.text have value @"  \n"
// then trimmedString will have value @""

if (trimmedString.length == 0) {
    // Empty field
}
Hani Ibrahim
  • 1,448
  • 11
  • 21
0

Try the following code:

 if (!self.foodName.text){
NSLog(@"Text is nil")
}
else if(self.foodName.text.length == 0){
    NSLog(@"Text is not nil but it does not contain anything");
}

if string does not contain any letter (lenght = 0) that does not mean that your string is nil. nil means your string is not allocated to any memory location.

Ankush
  • 2,405
  • 3
  • 22
  • 45
0

There are plenty of methods

if([self.foodName.text isEqualToString:@""] || [self.foodName.text isEqual:[NSNull null]]|| self.foodName.text.length == 0)  
{
  //code here
}
Aravind G S
  • 392
  • 3
  • 17
0

The shortest way is:

if(!self.foodName.text.length){
   NSLog(@"No Value")

}

It allows you to check following cases at once:

  1. self.foodName.text is nil (yes, you can check property on nil object)
  2. self.foodName.text is equal to @""
Artur Kucaj
  • 1,071
  • 1
  • 11
  • 16
0

Your code checks whether self.foodName.text is nil. Which means there is no text object. That is most likely not what you actually wanted to check. There is a difference between "no NSString object" and "an NSString object containing no characters at all".

If you want to check whether text is either no object at all, or a real object but with no text inside, then the easiest is self.foodName.text.length == 0. Perhaps you wanted to do something more complicated, like checking if there is no text, or only space characters, then the answer above using stringByTrimmingCharactersInSet will do this.

Both the simple check here and the more complicated one with stringByTrimmingCharactersInSet use a very common Objective-C technique: If you send a method to a nil object, or try to access a property of a nil object, the result is 0 / NO / nil, whatever is appropriate. So if text == nil, then text.length == 0. stringByTrimmingCharactersInSet returns nil, and nil.length again is 0, so you don't have to check for this.

gnasher729
  • 151
  • 1