Check if property of object instance is 'blank'
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.userNam开发者_开发问答e);
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.
I'd prefer
if([thisPhoto.userFullName length])
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];
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.
}
If, as I suppose, thisPhoto.userFullName
is a NSString
you may try
[thisPhoto.userFullName isEqualToString:@""]
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]
// this assumes userFullName and userName are strings and that userName is not nil
thisUserNameLabel.text = [thisPhoto.userFullName length] > 0 ? thisPhoto.userFullName : thisPhoto.userName;
"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!
}
精彩评论