Vik
Vik

Reputation: 486

What happens to Objective C method with NULL argument?

I am new to iOS programming and try to learn some of the basics and core concepts. I know that if we call a method on a NULL object, it does not give any error but it's just that the method won't get called. But what if I have a valid object and I am calling a method on it which takes an argument and that argument is null at the time of its calling. What would happen in that case?

e.g.

[self callMethod1:methodArgument];  //methodArgument is null at this time

-(void) callMethod1:(NSString *)methodArgument
{
  //Do Stuff here
}

Also, what would happen in the same situation if we are using C/C++ ?

Any answers/ideas would be appreciated.

Thanks Vik

Upvotes: 0

Views: 3010

Answers (1)

Georg Fritzsche
Georg Fritzsche

Reputation: 99044

There is nothing special about this case, it will just call the method with nil/0 as the argument.
In C++ the behaviour is the same: if you pass a null pointer, the method gets a null pointer as the argument.

Passing nil (which is just the name commonly used for null pointers to Objectice-C objects) is meaningful, consider e.g.:

[someClass setDelegate:nil]; // unregister delegate

Remember that you are passing pointers, not instances.

Upvotes: 3

Related Questions