The -
designates an instance method, whereas if it were a +
it would be a class method.
The (id)
is what the method will return, which is simply a reference to an object.
The rest of the line shows the parameters. When calling the function, you write out the part of each parameter before the :
, such as [class initWithTitle:@"my title"];
The reason why there are two names for each parameter is because the method itself will refer to the variable by whatever is after the :
, so the title will be newTitle
.
This was confusing to me at first, but there are advantages to it.
Also, the parts of each parameter inside parenthesis are the object type of the parameter. (NSString *)
is a pointer to a NSString
. If you were to pass something that wasn't an NSObject
, such as an NSIntger
, you would not need the *
. You'd simply do:
-(id)initWithInteger:(NSIntger)newInteger;