Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-08-2010, 06:04 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 2
Default Basic question about protocol syntax

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.

Last edited by creig; 02-08-2010 at 06:06 PM. Reason: code syntax unintentially converted to emoticons
creig is offline   Reply With Quote
Old 02-08-2010, 07:08 PM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: ¿La Islas Hermosas?
Posts: 1,426
Default

Quote:
Originally Posted by creig View Post
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.
__________________
piña para la niña interactive
Kalimba is offline   Reply With Quote
Old 02-08-2010, 08:17 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 2
Default Got it... I think.

It took several close readings for things to sink in, but I think I am beginning to understand. The idea that the method name is the entire set of parts is new to me. I saw a simple example in the documentation setting a position with an X and a Y. It just did not click that what I was seeing in the UIApplicationDelegate protocol was similar but with a more complex name. I just saw "application:" in front of half a dozen function names and could not figure out what it was doing there.

Thanks for the insight.
creig is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,243
Threads: 39,018
Posts: 171,143
Top Poster: smasher (2,570)
Welcome to our newest member, iWalter
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:09 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0