I’m developing an iOS 4 application using latest SDK and XCode 4.2.
I have enabled ARC on my project, and I’m trying to migrate a previous project to a new one that uses this feature (ARC).
The problem comes from an setter implementation. Here is my class (old version):
@interface RouteView : MKAnnotationView
{
/**
*/
MKMapView* _mapView;
...
}
@property (nonatomic, retain) MKMapView* mapView;
And its implementation:
@implementation RouteView
@synthesize mapView = _mapView;
-(void) setMapView:(MKMapView*) mapView
{
[_mapView release];
_mapView = [mapView retain];
[self regionChanged];
}
I get two compiler errors in the two first line on setMapView: method.
How can I do a custom setter method with ARC enabled?
With ARC, you no longer need to release/retain objects, as it has automatic reference counting that puts retain and release calls in at compile time.
As a quick fix when merging an old program, you can comment out any lines that have:
[myObject retain][myObject release]Just make sure that you don’t remove any functionality when you remove that part. In your application, you would need to replace
[mapView retain]withmapView, as you still need to set the object, just without retaining it.So your
setMapViewmethod would look something like this: