2

I am trying to implement the code below without success. Basically, I want to set the display name to use thisPhoto.userFullName if it is not 'Blank", else show thisPhoto.userName instead.

UILabel *thisUserNameLabel = (UILabel *)[cell.contentView viewWithTag:kUserNameValueTag];

NSLog(@"user full name %@",thisPhoto.userFullName);
NSLog(@"user name %@",thisPhoto.userName);
if (thisPhoto.userFullName && ![thisPhoto.userFullName isEqual:[NSNull null]] ) 
{
   thisUserNameLabel.text = [NSString stringWithFormat:@"%@",thisPhoto.userFullName];
}
else if (thisPhoto.userFullName == @"")
{
   thisUserNameLabel.text = [NSString  stringWithFormat:@"%@",thisPhoto.userName];
}

Currently, even if userFullName is blank, my userName is still not displayed on the screen.

1
  • 1
    Remember that if you use == with objects it compares their pointers. See below for more details on the methods you should use. Commented Jun 2, 2011 at 19:59

7 Answers 7

5

I'd prefer

if([thisPhoto.userFullName length])
Sign up to request clarification or add additional context in comments.

1 Comment

I know "zero means false" and "non-zero means true", but this type of code makes me shriek because it makes the intent ambiguous. When someone other than its author looks at that they will say: "Did the author forget to compare that value to another integer?". Omitting the right hand side of the comparison in this case, is IMHO ugly code.
3

Use -length. This will be 0 whenever the string is nil or the empty string @"". You generally want to treat both cases identically.

NSString *fullName = [thisPhoto userFullName];
thisUserNameLabel.text = [fullName length]? fullName : [thisPhoto userName];

Comments

2

I see a few points here

First - if your userFullName instance variable is NSString* then doing simple comparison with nil is enough:

if (thisPhoto.userFullName)

Unless, of course, you explicitly set it to be [NSNull null], which then requires the condition you wrote.

Second - comparing strings is done with isEqualToString: method so second condition should be rewritten as:

if ([thisPhoto.userFullName isEqualToString:@""]) {
    ...
}

Third - there's logic flaw - If your userFullName IS equal to empty string (@"") the code would still fall to the first branch. I.e. empty string (@"") is not equal to [NSNull null] or simple nil. Hence you should write to branches - one to handle empty string and nil, other one for normal value. So with a bit of refactoring your code becomes like this:

thisUserNameLabel.text = [NSString stringWithFormat:@"%@",thisPhoto.userFullName];
if (!thisPhoto.userFullName || [thisPhoto.userFullName isEqualToString:@""]) {
    // do the empty string dance in case of empty userFullName.
}

Comments

2

If, as I suppose, thisPhoto.userFullName is a NSString you may try

[thisPhoto.userFullName isEqualToString:@""]

Comments

2

The other two answers are correct, and beat me to it. Rather than just repeat what they have said - I'll point out something else.

[NSNull null] is used to store nil values in collection classes (NSArray, NSSet, NSDictionary) that don't allow nil values to be stored in them.

So unless you're checking values that you get from a collection - there is no point checking against [NSNull null]

Comments

1
// this assumes userFullName and userName are strings and that userName is not nil
thisUserNameLabel.text = [thisPhoto.userFullName length] > 0 ? thisPhoto.userFullName : thisPhoto.userName;

Comments

1

"Blank" means @"", but also @" " or @"\n". So I would trim userFullName and check the length of that string.

if ([[thisPhoto.userFullName stringByTrimmingCharactersInSet:
        [NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {

    // it's blank!
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.