Automatic @synthesis and refactoring

With the addition of automatic property synthesis to Objective C, the @synthesize statement is usually no longer necessary. However, the XCode refactoring operation has not been appropriately updated. Renaming ‘foo’ to ‘bar’ in the code below works – the property gets renamed to bar, but it remains linked to an ivar called _foo. Remove the @synthesize statement, however, and refactoring breaks as the implicit ivar will – after refactoring – now be called _bar, but the init body will still try to assign to _foo.


@interface Test
@property id foo;
@end
@implementation Test
@synthesize foo = _foo;
- (id)init {
   if ((self = [super init])) {
      _foo = self;
   }
   return self;
}
@end

Leave a Reply

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