Jim
Jim

Reputation: 5960

Objective-C Difference between (null) and nil

I've looked at some of the previous answers here to a similar question, but I still don't understand.

Here is the code (from ShareKit)

if (isDismissingView)
  return;

NSLog(@"presentingViewController: %@", [self.currentView presentedViewController]);

if (self.currentView != nil)
{
  // Dismiss the modal view
  if ([self.currentView parentViewController] != nil)
  {
    self.isDismissingView = YES;
    [[self.currentView parentViewController] dismissModalViewControllerAnimated:animated];
  }
  // for iOS5
  else if ([self.currentView respondsToSelector:@selector(presentingViewController)] && [self.currentView presentingViewController]) {     
    //****** it executes this block ******
    self.isDismissingView = YES;            
    [[self.currentView presentingViewController] dismissViewControllerAnimated:animated completion:^{  ...  }
  }
  else
    self.currentView = nil;
}

At the NSLog, the result is (null), which apparently is not the same as nil, because the block that tests if it is not nil

else if ([self.currentView respondsToSelector:@selector(presentingViewController)] && [self.currentView presentingViewController])

is executed.

So I have three questions. What is (null)? How does an object or pointer become (null)? What is the right way to test this condition?

Upvotes: 0

Views: 434

Answers (3)

user81434
user81434

Reputation:

NSLog() prints "(null)" when you give it a nil value for the object format %@. You can verify this by looking at the output of

NSLog(@"%@", nil);

So [self.currentView presentedViewController] is indeed nil. The else if test is looking at present*ing*ViewController, not present*ed*ViewController.

Upvotes: 4

rob mayoff
rob mayoff

Reputation: 385500

You're logging presentedViewController but testing presentingViewController.

Upvotes: 2

Pfitz
Pfitz

Reputation: 7344

nil is for objects and null for non-objects though I think that NSLog print null as description of the object even if it means nil. Have you tried to set a breakpoint and check what the debugger says at this point of code?

Upvotes: 2

Related Questions