I’m new to Objective-C, but please help me out here.
What I’m seeing is that method signatures in Objective-C that take multiple parameters seem inconsistent.
Example:
- (void) sendEmail: (NSString *) toStr
subject:(NSString *) subjectStr
body:(NSString *) bodyStr;
OK, so here we have 3 “parameters” (at least that’s what I’m used to calling them), but 2 of them have “external” names (subject, body) but the first one doesn’t! Isn’t there something wrong with that from a consistency/style point of view?
When we call this method we do it like:
[emailObj sendEmail:@"test@test.com" subject:@"testSub" body:@"this is a test"]
(Hopefully I did that right, remember I’m new to this.)
So the subject and the body are clearly marked in the call, but not the “to” address? It seems really wacked to me. Why is that first parameter getting special treatment?
I guess you thought that in the method declaration
Ais the method name,BandCare the names of parameters.In Objective-C, the totality
A:B:C:is the method name (more technically, called the selector) and used as a unit when you call a method by name. For example,checks if
objresponds toA:B:C:. But[obj respondsToSelector:@selector(A:)]will beNOin this case.So, you should really think of the totality of
A:B:C:as the method name, andAis the name of the first parameter.Note also that you can’t call
A:B:C:asA:C:B:, either.