NSMutableAttributedString检查nil值

时间:2015-10-27 12:12:23

标签: ios objective-c nsmutableattributedstring

我创建了一个NSMutableAttributedString,并且我动态地附加了该值。我需要检查此字符串是否为nil。我手动分配了nil,但下面的条件仍然失败。有没有办法检查这个?

 attribStr = [[NSMutableAttributedString alloc] init];
 attribStr = nil;
 if([attribStr isEqualToAttributedString:nil]) { // doesn't execute}

4 个答案:

答案 0 :(得分:2)

看起来你开始使用Objective-C: - )

当您向nil发送消息时(通过向isEqualToAttributedString:发送attribStr消息,您正在执行此操作),它会立即返回0。所以你的情况总是错误的。

如果您想检查attribStr是否为nil,请执行以下操作:

if (attribStr == nil) {
    // ...
}

答案 1 :(得分:1)

简单地:

if (!attribStr) {
    // nil
}

答案 2 :(得分:0)

而不是这个

if([attribStr isEqualToAttributedString:nil])

这样做

if(attribStr){
//means attribStr contains non nil value
}else{
//contains nil
}

答案 3 :(得分:0)

Vijay将代码更改为attribStr == nil

NSMutableAttributedString *attribStr = [[NSMutableAttributedString alloc] init];
attribStr = nil;
if(attribStr == nil)
{
  NSLog(@"The strig is nil");
}