【问题标题】:Check if property of object instance is 'blank'检查对象实例的属性是否为“空白”
【发布时间】:2011-09-07 08:34:15
【问题描述】:

我正在尝试实现以下代码但没有成功。基本上,如果不是“空白”,我想将显示名称设置为使用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仍然没有显示在屏幕上。

【问题讨论】:

  • 请记住,如果您将== 与对象一起使用,它会比较它们的指针。有关您应该使用的方法的更多详细信息,请参见下文。

标签: objective-c cocoa-touch ios uilabel string-comparison


【解决方案1】:

我更喜欢

if([thisPhoto.userFullName length])

【讨论】:

  • 我知道“零表示错误”和“非零表示正确”,但这种类型的代码让我尖叫,因为它使意图模棱两可。当作者以外的人看到它时,他们会说:“作者是否忘记将该值与另一个整数进行比较?”。在这种情况下省略比较的右侧是恕我直言的丑陋代码。
【解决方案2】:

使用-length。只要字符串为nil 或空字符串@"",这将为0。您通常希望对这两种情况一视同仁。

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

【讨论】:

    【解决方案3】:

    我在这里看到了几点

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

    if (thisPhoto.userFullName)
    

    当然,除非您明确将其设置为 [NSNull null],这需要您编写的条件。

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

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

    第三 - 存在逻辑缺陷 - 如果您的 userFullName 等于空字符串 (@""),代码仍会落入第一个分支。 IE。空字符串 (@"") 不等于 [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.
    }
    

    【讨论】:

      【解决方案4】:

      如果我想,thisPhoto.userFullNameNSString,你可以试试

      [thisPhoto.userFullName isEqualToString:@""]
      

      【讨论】:

        【解决方案5】:

        另外两个答案是正确的,并且击败了我。与其只是重复他们所说的话 - 我会指出其他内容。

        [NSNull null] 用于将 nil 值存储在不允许将 nil 值存储在其中的集合类(NSArrayNSSetNSDictionary)中。

        因此,除非您检查从集合中获得的值 - 否则检查 [NSNull null] 是没有意义的

        【讨论】:

          【解决方案6】:
          // this assumes userFullName and userName are strings and that userName is not nil
          thisUserNameLabel.text = [thisPhoto.userFullName length] > 0 ? thisPhoto.userFullName : thisPhoto.userName;
          

          【讨论】:

            【解决方案7】:

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

            if ([[thisPhoto.userFullName stringByTrimmingCharactersInSet:
                    [NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
            
                // it's blank!
            }
            

            【讨论】:

              猜你喜欢
              • 2014-08-22
              • 2020-09-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-05-06
              • 1970-01-01
              相关资源
              最近更新 更多