-2

I created a NSMutableAttributedString and I'm appending the value dynamically. I need to check if this string is nil or not. I manually assigned nil but still the condition below fails. Is there any way to check this?

 attribStr = [[NSMutableAttributedString alloc] init];
 attribStr = nil;
 if([attribStr isEqualToAttributedString:nil]) { // doesn't execute}
Marcos Crispino
  • 8,018
  • 5
  • 41
  • 59
Vijay
  • 355
  • 1
  • 5
  • 19

4 Answers4

2

Looks like you're getting started with objective-C :-)

When you send a message to nil (which is what you are doing by sending the isEqualToAttributedString: message to attribStr) it immediately returns 0. So your condition is always false.

If you want to check if attribStr is nil, simply do :

if (attribStr == nil) {
    // ...
}
deadbeef
  • 5,409
  • 2
  • 17
  • 47
0

Simply:

if (!attribStr) {
    // nil
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

Instead of this

if([attribStr isEqualToAttributedString:nil])

do this

if(attribStr){
//means attribStr contains non nil value
}else{
//contains nil
}
KavyaKavita
  • 1,581
  • 9
  • 16
0

Vijay change the code to attribStr == nil

NSMutableAttributedString *attribStr = [[NSMutableAttributedString alloc] init];
attribStr = nil;
if(attribStr == nil)
{
  NSLog(@"The strig is nil");
}
user3182143
  • 9,459
  • 3
  • 32
  • 39