Objective-C: Don’t make this mistake

Duct Tape, fixer of all things!It’s the little things that can drive a developer to jumping out a window. You plan a solution, code the solution, build the solution, run it, and… it doesn’t work. Most of the time you spot the problem right away, some times you’re left scratching your head, repeating over and over “this looks like it should work?” Of course you know better. It didn’t work, you just can’t see why. I had this problem last night so I thought I’d share with the world, ok not the world, just my electronic brain, so I can look it up later if necessary.

Here’s what I did.

@protocol Blah<NSObject>

Then I implemented the protocol.

@interface NeatoStuff : OtherStuff<Blah>

Later, in a different hunk of code I want to send a message to that implementation, so I did something I’ve now done many, many, times. I check to see if the delegate I’ve been given actually answers the message(implements the method) I’d like to send.

SEL delegateSelector = @selector(somethingHappened:);
if ([_delegate respondsToSelector:delegateSelector]) 
    [_delegate performSelector:delegateSelector];

It didn’t work, and I couldn’t see why. Can you? I’ll wait, go ahead and look it over again, I’ll play some Jeopardy music while you work.

Did you spot the problem? It’s one little character. This…

SEL delegateSelector = @selector(somethingHappened:);

Should be this…

SEL delegateSelector = @selector(somethingHappened);

Yep, that’s it. One little “:” had me pulling my hair out, and I just couldn’t see the problem to save my life, at least not for about 10-minutes of outright frustration.

The message doesn’t expect to receive arguments, so the “:” is not necessary. Note to self: Don’t do that again.

By Rob Fahrni

Husband / Father / Developer

2 replies on “Objective-C: Don’t make this mistake”

Comments are closed.