Objective c 在启用圆弧的情况下更新属性
我正在使用最新的SDK和XCode 4.2开发一个iOS 4应用程序 我已经在我的项目上启用了ARC,我正在尝试将以前的项目迁移到使用此功能的新项目(ARC) 问题来自setter实现。这是我的课程(旧版本): 及其实施:Objective c 在启用圆弧的情况下更新属性,objective-c,ios,xcode,memory-management,Objective C,Ios,Xcode,Memory Management,我正在使用最新的SDK和XCode 4.2开发一个iOS 4应用程序 我已经在我的项目上启用了ARC,我正在尝试将以前的项目迁移到使用此功能的新项目(ARC) 问题来自setter实现。这是我的课程(旧版本): 及其实施: @implementation RouteView @synthesize mapView = _mapView; -(void) setMapView:(MKMapView*) mapView { [_mapView release]; _mapView
@implementation RouteView
@synthesize mapView = _mapView;
-(void) setMapView:(MKMapView*) mapView
{
[_mapView release];
_mapView = [mapView retain];
[self regionChanged];
}
我在setMapView:
方法的两个第一行中得到两个编译器错误
如何在启用ARC的情况下执行自定义setter方法?您只需使用ARC即可:
-(void) setMapView:(MKMapView*) mapView
{
_mapView = mapView; // direct assignment to a strong ivar; let ARC do its thing
[self regionChanged];
}
使用ARC,您不再需要释放/保留对象,因为它具有自动引用计数功能,可以在编译时将retain和release调用放入 作为合并旧程序的快速修复,您可以注释掉具有以下内容的任何行:
[myObject retain]
[myObject发布]
[mapView retain]
替换为mapView
,因为您仍然需要设置对象,而不需要保留它
因此,您的setMapView
方法如下所示:
-(void) setMapView:(MKMapView*) mapView
{
//[_mapView release]; //Don't need this line anymore.
//_mapView = [mapView retain]; //Don't need this line either.
_mapView = mapView //You still want to set the mapView, just not retain it.
[self regionChanged];
}
“它有自动垃圾收集”这是不真实的。它具有自动引用计数功能,这与垃圾收集完全不同。圆弧插入在编译时保留并释放到代码中。垃圾收集在运行时发生,并监视对象图中不再使用的实体。这是一个重要的区别。@JackLawrence哇!谢谢你的澄清,我编辑了我的答案。
-(void) setMapView:(MKMapView*) mapView
{
//[_mapView release]; //Don't need this line anymore.
//_mapView = [mapView retain]; //Don't need this line either.
_mapView = mapView //You still want to set the mapView, just not retain it.
[self regionChanged];
}