4

Possible Duplicate:
! vs == nil in objective-c

In Objective C, is there any difference between the following two conditionals?

NSObject *obj; if(!obj) { ... }

And:

NSObject *obj; if(obj == nil) { ... }

Thanks! Just curious, figure it's a good thing to know.

Community
  • 1
  • 1
Mason
  • 6,893
  • 15
  • 71
  • 115

2 Answers2

2

There is no difference between the two forms.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
-1

!obj also is valid when obj=NULL or obj=@"" or empty Array etc. You get the point.

i.e. !obj is more comprehensive since you seem to be using NSObject & not any specific object type.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • 3
    `!obj` is *not* true ("valid"?) when `obj` is `@""` or an empty array. Also, `nil` is the conventional way to denote an empty object reference, not `NULL`. – Marcelo Cantos Aug 10 '11 at 14:58
  • 3
    `!obj` is **not** true when `obj=@""` nor when it's an empty `NSArray` object. `@""` is a pointer to an `NSString` object with a length of `0`. That pointer is never `NULL` (or `nil`) since it's allocated at compile-time. –  Aug 10 '11 at 14:59
  • want to delete this! I guess thats not possible... – Srikar Appalaraju Aug 11 '11 at 02:44