Android/Kotlin and Python classes Registration Deadline Approaching

Real iPhone Crap 3: Where’s the @override keyword?

Aaron Hillegass

This one is not really crap; it is just a sad omission in the Objective-C 2 language.

I spend a lot of time debugging other people’s code, and a surprisingly common error is a misspelled delegate or datasource method. Often I’ll see a method like this:

- (NSInteger)numberOfSectionsInTableview:(UITableView *)tv
{ ... }

And I’ll stare at the method for 15 minutes trying to figure out why the method isn’t getting called. (The “v” should be uppercase.)

With Objective-C 2, Apple did something great: The @optional tag on protocols. Now the delegate or datasource method could be declared properly in a protocol:

@protocol UITableViewDataSource <NSObject>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tv;

And I can specify that my class is a table view data source by declaring that it conforms to the protocol:

@interface CrapController : UIViewController <UITableViewDataSource>

But, there is still no way to get the compiler to catch my misspelled data source method.

Our lives would be better if we had an @override keyword that would say to the compiler “The next method is previously declared in the header file of one of my ancestors or in one of the protocols that I conform to.” If the compiler couldn’t find the declaration, it would give you a stern warning. This code, for example, would generate such a warning:

- (NSInteger)numberOfSectionsInTableview:(UITableView *)tv
{ ... }

A small check like this, combined with the new optional protocols, would make a whole class of errors obvious at compile time rather than at run time.

(Yes, I’ve suggested this to the compiler guys at Apple. Perhaps this posting will help it become part of the collective unconscious that will form Objective-C 3.)

Not Happy with Your Current App, or Digital Product?

Submit your event

Let's Discuss Your Project

Let's Discuss Your Project