Search

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

Aaron Hillegass

2 min read

Aug 16, 2009

iOS

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

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>
@optional
...
- (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:

@override
- (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.)

Juan Pablo Claude

Reviewer Big Nerd Ranch

During his tenure at BNR, Juan Pablo has taught bootcamps on macOS development, iOS development, Python, and Django. He has also participated in consulting projects in those areas. Juan Pablo is currently a Director of Technology focusing mainly on managing engineers and his interests include Machine Learning and Data Science.

Speak with a Nerd

Schedule a call today! Our team of Nerds are ready to help

Let's Talk

Related Posts

We are ready to discuss your needs.

Not applicable? Click here to schedule a call.

Stay in Touch WITH Big Nerd Ranch News