Saturday, May 11, 2013

NIL Pointer in Objective C

In many programming languages, we do the checking if the pointer is null (equally, 0) or not before we start to grab the content. However, in Objective C, there's no need to do this. The design makes you no need to do so.

When a pointer has no object points to, it's automatically storing the value 0, which we call a NIL pointer. And, we can complete our other designs without checking the pointer is NIL or not because that function calls just do nothing on NIL pointers, and return 0.

Take one example I learned in the class,
if ([card.contents isEqualToString:self.contents] ){ score = 1; }
No need to card.contents is NIL or not. If it's NIL, isEqualToString just simply skip it and return 0, which may cause the if-statement failed, which is also the right thing we want.