0

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?

Jim
  • 5,940
  • 9
  • 44
  • 91
  • 2
    Have you tried Cmd+clicking null and nil to see their definitions? Also possible duplicate of [NULL vs nil in Objective-C](http://stackoverflow.com/questions/557582/null-vs-nil-in-objective-c) – CodaFi May 25 '12 at 04:16
  • 1
    I didn't see this as a duplicate, since that question, which I did look at, refers to `NULL`, not `(null)`. – Jim May 25 '12 at 04:31
  • Would you believe me if I told you they were the same thing? ;) – CodaFi May 25 '12 at 04:32
  • If you down voted my question for that reason, it would be nice if you reversed it. As far as I can tell, the definitions don't say anything about `(null)` and neither does the question you referred to. – Jim May 25 '12 at 04:52
  • This seems like a legitimate question. Given the poor answer you received, it should be clear what looks obvious isn't necessarily so. that some aren't paying close attention. (And we all get strained after a hard days work, so simple mistakes can be forgiven). `null` is not a defined value, but `NULL` is defined, and `nil` is defined as equivalent to `NULL`. And as someone pointed out in another related question `NIL` does not exist. There is a clear difference between null and NULL, then. Mark got it, that (null) is just the description of nil. But it's not obvious that's what's going on. – Draco May 25 '12 at 08:05

3 Answers3

4

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.

2

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?

Pfitz
  • 7,336
  • 4
  • 38
  • 51
2

You're logging presentedViewController but testing presentingViewController.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Thanks, Rob and Mark. It must be getting late here. I see it can autocomplete both ways. – Jim May 25 '12 at 04:37