Quote:
Originally Posted by creig
I am a complete noob about Objective-C. My background is in C and Java, and I am just starting to learn how to develop for the iPhone. I am trying to understand some of the concepts that don't easily map to APIs I understand.
In the iPhone SDK class UIApplication, it creates a UIApplicationDelete protocol that includes the following two methods:
- (void)applicationWillResignActive: (UIApplication *)application;
- (BOOL)application: (UIApplication *)application handleOpenURL: (NSURL *)url;
I understand the first syntax as it is basically (return type) functionName: functionArgument.
I am struggling to understand the syntax of the second method. If handleOpenURL is the method name, what is the role of "application: (UIApplication *)application"? The documentation indicates "application" is a parameter, but I don't understand why it appears between the return type and the method name.
I have not been able to find out what the syntax means or even whether I am comprehending it correctly. And since am just beginning, I don't have the vocabulary mastered enough to know what terms to search for.
Thanks.
|
As someone who had been writing C/C++ code for decades before diving into Obj-C, I feel your pain. IMO, learning how to "see" the syntax of Obj-C functions is one of the most difficult hurdles to get over when starting in this language.
In virtually every other "popular" programming language, function declarations have a return type, followed by the full function name, followed by a parameter list. In Obj-C, however, the function name and the parameter list actually interleave each other when the function takes more than 1 argument. When the function takes 1 or fewer arguments, the function syntax should look similar to other languages, as in the case of the first function declaration you posted.
Let's break down the second function declaration:
Code:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;
This is a class instance function/method, because that's what the "-" symbol indicates.
The return type follows, in parentheses. This function will return a BOOL data type.
What follows is an interleave of function name "parts", and function parameters. The name part includes everything up to and including the colon ":". A parameter type in parentheses is next and the parameter name follows. This pattern is repeated for as many function parameters there are. The actual function name is built by concatinating all of the name "parts".
In this instance, the actual function name is "application:handleOpenURL:". The function accepts two parameters, a UIApplication * which is named "application" and an NSURL * named "url".
The first parameter named "application" is entirely coincidental to the fact that the first function name part is "application:". This parameter can simply be named "app", and the function declaration will then look like:
Code:
- (BOOL)application:(UIApplication *)app handleOpenURL:(NSURL *)url;
Hope this helps.