Got right confused with the didFailWithError declaration


#1

Hey all,

So I was working quite well with the idea that method calls were structured in the following way:

-+ (A) B (C *)D;

Where
+/- = indicates whether this is a class or instance method
A = The return type of the method
B = The method name
C = Input Parameter type
D = Input Parameter name

This worked well for me for 24 chapters… :slight_smile:

Then this happened:
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

Can I please get an explanation as to where the “connection:(NSURLConnection *)connection” portion came from? I am expecting that is has to do with the protocol outlined by the NSURLConnection… but protocols are in chapter 25. (haha)

I guess that my question is… should I be worried that I don’t understand the syntax to the “didFailWithError” method declaration? (And I notice that “connectionDidFinishLoading” is structured as I would have expected… why the difference?)

Thx,
Rana


#2

Your summary of method-declaration syntax is not complete.

If you keep in mind that method declarations have the following syntax:

[color=#FF0000]MethodDeclSyntax[/color] : MethodType (ReturnType) LabelledParameterList

MethodType : - or + // instance or class method

ReturnType : Type

Type :

(1) LabelledParameterList :
(2) LabelledParameterList : [ ‘:’ ‘(Type ')’]+

where […]+ means repeat ‘…’ one or more times; and ‘.’ means enter ‘.’ literally, without the quotes.

Then you can tell that the following declarations are all valid:

By (1): + (Bar)bar
By (1): - (Bar)bar
By (2): - (void)setBar:(Bar *)po
By (2): - (void)tellBar:(Bar *)po toFrizzle:(int)times

And of course:
By (2): - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error


#3

So after a big of digging I am going to take the idea on this is how we pass in multiple parameters into a method call.

However would it not be much easier to understand it if the method was written as:

  • (void) didFailWithError:(NSError *)error connection(NSURLConnection *) connection -?

Then I know the first label after my return type is the name of my method… I would also guess that the order of the parameters is very important and dictates the selector of the method (Java overloading), so I can’t move them around…

Is it objective C Standard that the last parameter is the name of the method?

Thanks for the help… my brain was leaking a bit out of my ears there… :slight_smile:
R


#4

[quote]Is it objective C Standard that the last parameter is the name of the method?
[/quote]
No. The name of the method is obtained by concatenating all the labels, including colons, in the order they appear in the method declaration. For example: connection:didFailWithError:

[quote]However would it not be much easier to understand it if the method was written as:

  • (void) didFailWithError:(NSError *)error connection(NSURLConnection *) connection -?
    [/quote]
    The expected norm is to start with the object that generated the error; however, you can set your own norm if your are the king.

#5

Got it!

Thanks for the help.
R