-2

I am currently trying to check the length of a label.

What it is is i want the label to display "Unavailable" if the string is of a null value.

The sting is being read in from a XML sheet so i don't know what the length of the actual string is and i would like to know. This is what i currently have but its not working.

It displays the relevant text if its not empty which is brilliant.

But its not displaying the text when it is empty which is leading me to believe although i assume its empty its not.

Thanks in advance.

if ([l_subCommunity.text length] > 0)
{
    [l_subCommunity setText:_property.str_subCommunity];
    NSLog(@"%",l_subCommunity);
}
else
{     
    NSMutableString *sub = [[NSMutableString alloc]init];
    [sub appendString:@"Unavailable"];
    [self.l_subCommunity setText:sub];
}

2 Answers2

1
[l_subCommunity setText:_property.str_subCommunity];

[self.l_subCommunity setText:sub];

you are using l_subCommunity setText in the if and self.l_subCommunity setText in the else. are you using 2 different variables?

Also why are you creating a mutable string to pass in the value @"Unavailable" ?

why not simply:

[self.l_subCommunity setText: @"Unavailable" ];
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
  • If I use [self.l_subcommunity setText: @"Unavalible"] then doesn't it stay statically like that, because i want it to change depending on wether the sting is empty or not. – user3083379 Dec 13 '13 at 13:45
0

Your if statement is checking the wrong variable. You want:

if (_property.str_subCommunity.length) {
    l_subCommunity.text = _property.str_subCommunity;
    NSLog(@"%",l_subCommunity);
} else {     
    self.l_subCommunity.text = @"Unavailable";
}

Also keep in mind that you may end up with whitespace and/or newlines in your string as a result of parsing the XML file. You may need to trim this whitespace from your string.

rmaddy
  • 314,917
  • 42
  • 532
  • 579