Optional parameters in methods in Objective C

Methods in Objective C are actually called messages but they’re often referred to as methods, a more commonly used terminology.

Optional parameters are treated in an interesting way in Objective C. Basically there are no optional parameters! In fact the parameters help to define the method.

To illustrate this, rather than the following method being the ‘printFirstName’ method, it is actually best envisaged as the ‘printFirstName:surname:’ method.

-void printFirstName:(NSString *)firstName surname:(NSString *)surname;

To explain: ‘surname:(NSString *)surname’ – the first ‘surname’ is the parameter name as seen external to the method, the second ‘surname’ is the variable name for the parameter within the method.

In Actionscript for example you can have a method with an optional parameter:

function printThings(firstThing:String,secondThingOptional:String=’’):void

So how to do something similar in Objective C? You can create two methods, one without the optional parameter, and one with the optional parameter. The method without the optional parameter can then call the method with the optional parameter specifying the default. So we would could something like the following:

-void printFirstName:(NSString *)firstName surname:(NSString *)surname; {
	//...
}
-void printFirstname:(NSString *)firstName; {
	[self printFirstName:firstName surname:@""];
}

The two methods are called ‘printFirstname:’ and ‘printFirstname:surname:’. At first glance, the second method seems to be called ‘printFirstName’ which doesn’t make sense as it also prints surname. However, ‘surname:’ is actually part of the name of the method. (it also externally names the second parameter)

For more on optional parameters, I’ve taken a look at them in Swift here.

Interactive developer, Author - iOS development with Swift - book coming 2017 https://manning.com/books/ios-development-with-swift

Tagged with: ,
Posted in Objective C
2 comments on “Optional parameters in methods in Objective C
  1. […] This technique of optional parameters is consistent with the approach Objective C took and was necessary to include in Swift to be able to interpret underlying Objective C code. (I discussed optional parameters in Objective C in a previous blog post here). […]

  2. […] This technique of optional parameters is consistent with the approach Objective C took and was necessary to include in Swift to be able to interpret underlying Objective C code. (I discussed optional parameters in Objective C in a previous blog post here). […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: