检查对象实例的属性是否为“空白”
我正在尝试实现下面的代码,但没有成功。基本上,我想将显示名称设置为使用 thisPhoto.userFullName
(如果它不是“空白”),否则显示 thisPhoto.userName
。
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];
}
目前,即使 userFullName
为空,我的 userName
仍然没有显示在屏幕上。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我更愿意
I'd prefer
使用
-length
。当字符串为nil
或空字符串@""
时,该值将为 0。您通常希望以相同的方式处理这两种情况。Use
-length
. This will be 0 whenever the string isnil
or the empty string@""
. You generally want to treat both cases identically.我在这里看到几点
首先 - 如果您的
userFullName
实例变量是NSString*
那么与nil
进行简单比较就足够了:当然,除非,您显式地将其设置为
[NSNull null]
,然后需要您编写的条件。第二 - 比较字符串是使用
isEqualToString:
方法完成的,因此第二个条件应重写为:第三 - 存在逻辑缺陷 - 如果您的
userFullName
IS 等于空字符串 (@""
) 代码仍然会落到第一个分支。即空字符串 (@""
) 不等于[NSNull null]
或简单的 nil。因此,您应该写入分支 - 一个用于处理空字符串和 nil,另一个用于处理正常值。因此,通过一些重构,您的代码将变成这样:I see a few points here
First - if your
userFullName
instance variable isNSString*
then doing simple comparison withnil
is enough: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: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:如果,正如我所想,
thisPhoto.userFullName
是一个NSString
你可以尝试If, as I suppose,
thisPhoto.userFullName
is aNSString
you may try另外两个答案是正确的,并且打败了我。我不会只是重复他们所说的话,而是会指出一些其他的事情。
[NSNull null]
用于在集合类(NSArray
、NSSet
、NSDictionary)中存储
),不允许在其中存储nil
值nil
值。因此,除非您检查从集合中获取的值 - 没有必要检查
[NSNull null]
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 storenil
values in collection classes (NSArray
,NSSet
,NSDictionary
) that don't allownil
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]
“空白”表示
@""
,也表示@" "
或@"\n"
。所以我会修剪 userFullName 并检查该字符串的长度。"Blank" means
@""
, but also@" "
or@"\n"
. So I would trim userFullName and check the length of that string.