检查对象实例的属性是否为“空白”

发布于 2024-11-13 05:20:08 字数 726 浏览 3 评论 0原文

我正在尝试实现下面的代码,但没有成功。基本上,我想将显示名称设置为使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ˇ宁静的妩媚 2024-11-20 05:20:08

我更愿意

if([thisPhoto.userFullName length])

I'd prefer

if([thisPhoto.userFullName length])
卷耳 2024-11-20 05:20:08

使用-length。当字符串为 nil 或空字符串 @"" 时,该值将为 0。您通常希望以相同的方式处理这两种情况。

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

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];
清旖 2024-11-20 05:20:08

我在这里看到几点

首先 - 如果您的 userFullName 实例变量是 NSString* 那么与 nil 进行简单比较就足够了:

if (thisPhoto.userFullName)

当然,除非,您显式地将其设置为 [NSNull null],然后需要您编写的条件。

第二 - 比较字符串是使用 isEqualToString: 方法完成的,因此第二个条件应重写为:

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

第三 - 存在逻辑缺陷 - 如果您的 userFullName IS 等于空字符串 (@"") 代码仍然会落到第一个分支。即空字符串 (@"") 不等于 [NSNull null] 或简单的 nil。因此,您应该写入分支 - 一个用于处理空字符串和 nil,另一个用于处理正常值。因此,通过一些重构,您的代码将变成这样:

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

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.
}
意中人 2024-11-20 05:20:08

如果,正如我所想, thisPhoto.userFullName 是一个 NSString 你可以尝试

[thisPhoto.userFullName isEqualToString:@""]

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

[thisPhoto.userFullName isEqualToString:@""]
梦醒时光 2024-11-20 05:20:08

另外两个答案是正确的,并且打败了我。我不会只是重复他们所说的话,而是会指出一些其他的事情。

[NSNull null] 用于在集合类(NSArrayNSSetNSDictionary)中存储 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 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]

蛮可爱 2024-11-20 05:20:08
// this assumes userFullName and userName are strings and that userName is not nil
thisUserNameLabel.text = [thisPhoto.userFullName length] > 0 ? thisPhoto.userFullName : thisPhoto.userName;
// this assumes userFullName and userName are strings and that userName is not nil
thisUserNameLabel.text = [thisPhoto.userFullName length] > 0 ? thisPhoto.userFullName : thisPhoto.userName;
枯叶蝶 2024-11-20 05:20:08

“空白”表示 @"",也表示 @" "@"\n"。所以我会修剪 userFullName 并检查该字符串的长度。

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

    // it's blank!
}

"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!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文