Selectors in Objective-C

I ran into this one yesterday since I needed to create a delegate method that took a couple arguments, and it wasn’t clear to me what the correct syntax was when one had more than one. So thanks to Adam Rosenfield on Stackoverflow for this one (he helped clear up another issue I was researching a couple of weeks ago but I failed to blog about that one I think…).

Selectors in Objective C – Stack Overflow:

You have to be very careful about the method names. In this case, the method name is just “lowercaseString”, not “lowercaseString:” (note the absence of the colon). That’s why you’re getting NO returned, because NSString objects respond to the lowercaseString message but not the lowercaseString: message.

How do you know when to add a colon? You add a colon to the message name if you would add a colon when calling it, which happens if it takes one argument. If it takes zero arguments (as is the case with lowercaseString), then there is no colon. If it takes more than one argument, you have to add the extra argument names along with their colons, as in compare:options:range:locale:. You can also look at the documentation and note the presence or absence of a trailing colon.

Leave a comment

Your email address will not be published. Required fields are marked *