lampShade
lampShade

Reputation: 4391

What object is nil here?

In the following code I'm curious to know what object is being set to nil in the:

-(void)ViewWillAppear:(BOOL) method. Is self being nil'ed? or are all the objects in the

-(IBAction)showCurrentTime:(id)sender method being nil'ed, or are both being nil'ed?

-(IBAction)showCurrentTime:(id)sender
{
    NSDate *now = [NSDate date];
    static NSDateFormatter *formatter = nil;
    if (!formatter) {
        formatter = [[ NSDateFormatter alloc ] init ];
        [formatter setTimeStyle:NSDateFormatterShortStyle];

    }
    [timeLabel setText:[formatter stringFromDate:now]];

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self showCurrentTime:nil];


}

Upvotes: 1

Views: 92

Answers (2)

MarkPowell
MarkPowell

Reputation: 16530

I don't see anything being set to nil. If you are referring to the sender parameter, what is happening is nil is being PASSED as the sender object. This is not assigning any variable to nil. In your example, sender is never used, so nil has no effect.

However, if that parameter was used, you'd get the default behavior of passing messages to nil. That is, if the call is void, nothing happens, if it returns an object, nil is returned, etc.

Upvotes: 2

FreeAsInBeer
FreeAsInBeer

Reputation: 12979

If you're referring to the line [self showCurrentTime:nil];, then it's simply sending nil because the method requires the sender parameter. If there are no cases when you need the sender (even if you are using the method from Interface Builder) then it's OK to remove the ":(id)sender" part of the method name.

Upvotes: 2

Related Questions