views:

32

answers:

1

I had something like the following code in a project I recently worked on.

@interface DetailsViewController : UIViewContoller {
  UIView* headerView_;
}
@property (nonatomic, retain) UIView* headerView;
@end

@implementation DetailsViewController
@synthesize headerView = undefinedVariableName_;
// ...
@end

undefinedVariableName_ was not defined anywhere in the project and was actually a much less obvious typo.

This compiled perfectly fine (no errors or warnings) and even ran fine on iOS 4. I did not catch this error until the program crashed on 3.1.3 firmware.

Does anyone know if the above behaviour is considered undefined? Is there a way to have the compiler catch such mistakes?

+1  A: 

In the modern Objective-C runtime you don’t have to declare the ivars yourself, the compiler will create them for you at the point of @synthesize. If it crashed on the older iOS this version probably doesn’t support the modern runtime yet.

Sven
I believe that iOS has always used the modern runtime. http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ObjCRuntimeGuide/Articles/ocrtVersionsPlatforms.html
David M.
I think @David has a good point, but thanks @Sven for pointing out that the compiler can actually create ivars automatically.
Whisty