views:

76

answers:

3

Hi guys,

I'm having a problem with this:

-(id)initWithName:(NSString *)n description:(NSString *)d url:(NSString *)u {

I'm not really sure whats going on here, is the mehtod initWithName being passed an argument of n which is being casted to an NSString ? and is also being passed an argument d which is being casted to an NSString also?

What is the purpose of having the letters'n' and 'l' there? Do they need to be defined else where or are they just arbitrary argument names to help us remember what the arguments are?

Is this one method or three combined? Are 'description:' and 'url' methods too?

Sorry about this question being so basic, I'm just a little confused by this one.

Thanks,

+6  A: 

This is a method with the name "initWithName:description:url:" that takes arguments named n, d and u, all of type NSString*. Objective-C syntax splits up the method's name (called a "selector" in Objective-C terms) at each colon and you place a corresponding argument there. The point is to make code read more naturally. I will say that n, d and u are godawful names for arguments.

You might be interested in Apple's The Objective-C Programming Language. It's very short, but still manages to fully describe both the language and its philosophy.

Chuck
+1 "godawful names for arguments"
Thomas Müller
Thanks very much for all the reponses, it makes a lot more sense now. I really appreciate your time :)
Dave
+2  A: 

Sometimes it's easier to understand when some extra whitespace is added:

-(id)initWithName:(NSString *)n
      description:(NSString *)d
              url:(NSString *)u
{
    ...

Basically, this a method that takes three arguments, all are NSString arguments, and they are called n, d, and u respectively. It is important to note that the colon is part of the method name, but the argument types and argument names are not part of the name. The method's name is:

initWithName:description:url:
dreamlax
+2  A: 

It is one method. This is how objective-c passes multiple variables to a method. If you are more familiar with C it would look more like: initWithName(n,d,u).

However, in objective-c more information is given about each parameter in a method call. I think you will find that you will like this because it helps you understand the purpose of each parameter.

sammcd