Showing posts with label nil. Show all posts
Showing posts with label nil. Show all posts

Friday, June 3, 2011

Creating an Empty Array

To create an empty array use NSNull:

NSNull *myNull = [NSNull null];
NSMutableArray *arr = [NSMutableArray arrayWithObjects: myNull, myNull, nil];


Then to compare use something like:

if ([arr objectAtIndex:index]  == (id)[NSNull null]) {

  //do something
}


nil cannot be added into an Array or collection similar to how an int cannot be added.  NSNull is the helper object to get nil into the collection.  In Objective C there's nil, NSNull and Null.

NSNull, nil, null differences

Found a great answer on stack overflow.  Here's the gist of it:

nil is like (NSObject *)0
NULL is like (void*)0
Both are pointers with an integer value of zero.

NSNull is a helper object to add nil to NSArray or container classes requiring an object.  Similar to how NSNumber is needed to add an int.  [NSNull null] can be used instead of nil.  You can't compare NSNull to nil, but you can check if something is a NSNull.