Reputation: 751
I have a UILabel that is loaded form a text file. Sometimes the text file has something in it and sometimes it is empty. So sometimes the UILabel is blank and sometimes it has text in it.
I want to write an if statement that says if the UILabel is blank do one thing else if it has text in it do another thing.
I have tried
if (self.label.text = NULL)
and
if (self.label.text = @"")
but it isn't working correctly.
With the if (self.label.text = @"")
, I get the if statement to happen but the else statement doesn't work.
Here is my code
NSString *stuff3 = @"/Stuff";
NSString *titleName = [familyDictionary objectForKey:@"identity"];
NSArray *paths3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory3 = [paths3 objectAtIndex:0];
NSString *stuffPath3 = [documentsDirectory3 stringByAppendingPathComponent:stuff3];
NSString *fullPath3 = [stuffPath3 stringByAppendingPathComponent:titleName];
self.title = [NSString stringWithContentsOfFile:fullPath3 encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"full path 3 >>>%@",fullPath3);
NSString *stuff4 = @"/Stuff/Objects";
NSString *textName3 = [familyDictionary objectForKey:@"identity"];
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
NSString *stuffPath = [documentsDirectory2 stringByAppendingPathComponent:stuff4];
NSString *fullPath2 = [stuffPath stringByAppendingPathComponent:textName3];
self.wordlabel.text = [NSString stringWithContentsOfFile:fullPath2 encoding:NSUTF8StringEncoding error:NULL];
//Now load the image at fullPath and install it into our image view's image property.
NSLog(@"full path 3 >>>%@",fullPath2);
if(self.wordlabel.text = @"")
{
[textView setTitle:[NSString stringWithContentsOfFile:fullPath3 encoding:NSUTF8StringEncoding error:NULL] forState:UIControlStateNormal] ;
textView.titleLabel.adjustsFontSizeToFitWidth = TRUE;
}
else
{
[textView setTitle:[NSString stringWithContentsOfFile:fullPath2 encoding:NSUTF8StringEncoding error:NULL] forState:UIControlStateNormal] ;
textView.titleLabel.adjustsFontSizeToFitWidth = TRUE;
}
Upvotes: 1
Views: 3585
Reputation: 119242
You're doing it wrong, wrong.
Wrong 1: =
is the assignment operator, ==
is the equality operator. You're using the assignment operator inside an if statement, you should at least be getting a compiler warning about that.
Wrong 2: Even if you had that bit right, it's the wrong way to compare strings. Use isEqualToString:
or check length as in the other answers.
Wrong 3: The logic should probably be based on the strings before you assign them to the label, not by reading back what is in the label. It's a cleaner MVC implementation.
Upvotes: 3
Reputation: 118651
When you say self.label.text = @""
you are changing your label's text. =
is the assignment operator. ==
is the comparison operator.
However, to compare strings, you must use the comparison method [self.label.text isEqualToString:@""]
, otherwise you are just comparing pointers.
Upvotes: 1
Reputation: 1045
What you are doing is comparing pointers, which doesn't work with strings. Use this
if ([self.wordlabel.text isEqualToString:@"thestring"])
Upvotes: 1
Reputation: 8109
you should ideally use:
if ([self.label.text length] > 0)
Upvotes: 1